C# dataGridView绑定access数据库,界面更新自动同步到数据库
参考:http://www.cnblogs.com/hoosway/archive/2014/06/18/DataGridView.html
/// <summary>
/// 数据适配器
/// </summary>
OleDbDataAdapter adapter = null;
/// <summary>
/// 数据集对象
/// </summary>
DataSet dSet = null;
/// <summary>
/// 连接字符串
/// </summary>
private static string strConn = "Provider= Microsoft.Ace.OLEDB.12.0;Data Source = d:\\db.accdb";
public Form1()
{
InitializeComponent();
adapter = new OleDbDataAdapter("select ID, system, type, rDate, rLocation, rAccount, rPassword, rCiphertext, remark from Record", strConn);
dSet = new DataSet();
adapter.Fill(dSet);
dataGridView1.DataSource = dSet.Tables[0];
}
private void button1_Click(object sender, EventArgs e)
{
//创建命令重建对象
OleDbCommandBuilder scb = new OleDbCommandBuilder(adapter);
//更新数据
try
{
//这里是关键
adapter.Update(dSet);
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}