ข้างล่างคือ Code ข้างต้นที่ผมทำนะครับ ตัวที่ comment ไว้คือถ้าปลด comment ออกระบบรันแล้ว error
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace connect2tbl
{
public partial class Form1 : Form
{
private OleDbConnection connection = new OleDbConnection();
public Form1()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SK\Documents\Database2.accdb;
Persist Security Info=False;";
}
private void button1_Click(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = connection;
cmd.CommandText = "insert into Table1 (name) values ('"+ this.txtname.Text+"')";
//cmd.CommandText = "insert into Table2 (no,book_no) values ('"+this.txtno.Text+ "','"+this.txtbook.Text+"')";
//cmd.CommandText = "insert into Table3 (addname) values ('" + this.txtaddname.Text + "')";
cmd.ExecuteNonQuery();
MessageBox.Show("บันทึกเรียบร้อย");
connection.Close();
this.txtname.Text = "";
this.txtno.Text = "";
this.txtbook.Text = "";
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
}
}
วิธี insert into ข้อมูล 2 - 3 table ขึ้นไป ในหน้า form เดียวกันและปุ่มบันทึกเดียวกันทำยังไงครับ vs C# / access db
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace connect2tbl
{
public partial class Form1 : Form
{
private OleDbConnection connection = new OleDbConnection();
public Form1()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SK\Documents\Database2.accdb;
Persist Security Info=False;";
}
private void button1_Click(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = connection;
cmd.CommandText = "insert into Table1 (name) values ('"+ this.txtname.Text+"')";
//cmd.CommandText = "insert into Table2 (no,book_no) values ('"+this.txtno.Text+ "','"+this.txtbook.Text+"')";
//cmd.CommandText = "insert into Table3 (addname) values ('" + this.txtaddname.Text + "')";
cmd.ExecuteNonQuery();
MessageBox.Show("บันทึกเรียบร้อย");
connection.Close();
this.txtname.Text = "";
this.txtno.Text = "";
this.txtbook.Text = "";
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
}
}