C# Access数据库的表已经更改,dataGridView怎么更新
发布网友
发布时间:2022-04-09 10:27
我来回答
共1个回答
热心网友
时间:2022-04-09 11:56
C# Access数据库的表已经更改,dataGridView怎么更新
Adapter Ada = new OleDbDataAdapter();
DataTable table = (DataTable)DataList.DataSource;
//重新启动连接
String ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + "/TEST.mdb";
//用Buider方法更新数据
using (OleDbConnection connection = new OleDbConnection(ConnectionString))
{
Ada.SelectCommand = new OleDbCommand("SELECT * FROM TestACCESS", connection);
OleDbCommandBuilder builder = new OleDbCommandBuilder(Ada);
Ada.UpdateCommand = builder.GetUpdateCommand();
try
{
//更新数据表数据时
Ada.Update(table);
//----这个地方会把所有的更新同步到数据库中--------------------
table.AcceptChanges();
MessageBox.Show("操作已成功!数据将全部被保存......", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);
}
}