C#DataTable更新访问数据库
如何将DataTable
保存到文件中。 accdb(Access)现有的一个?我用下面的代码,它不工作:C#DataTable更新访问数据库
using (OleDbConnection oledbConnection = new OleDbConnection(connection))
{
oledbConnection.Open();
string query = "SELECT * FROM Student";
using (OleDbCommand oledbCommand = new OleDbCommand(query, oledbConnection))
{
using (OleDbDataAdapter oledbDataAdapter = new OleDbDataAdapter(oledbCommand))
{
using (OleDbCommandBuilder oledbCommandBuilder = new OleDbCommandBuilder(oledbDataAdapter))
{
oledbDataAdapter.DeleteCommand = oledbCommandBuilder.GetDeleteCommand(true);
oledbDataAdapter.InsertCommand = oledbCommandBuilder.GetInsertCommand(true);
oledbDataAdapter.UpdateCommand = oledbCommandBuilder.GetUpdateCommand(true);
oledbDataAdapter.Update(dataTable);
}
}
}
oledbConnection.Close();
}
变量数据表上初始化文件的原始内容,然后将其修改通过添加行,我现在有更新的表数据库。
我尝试使用下面的代码,但是,这并不工作:(
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM Student", connection);
OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(da);
da.InsertCommand = cmdBuilder.GetInsertCommand(true);
// create and insert row in the DataTable
da.Update(dataTable);
假设你已经在数据表中的一些变化,那么你可以通过生成的更新/插入/删除命令到适配器以这种方式
oledbDataAdapter.DeleteCommand = oledbCommandBuilder.GetDeleteCommand();
oledbDataAdapter.InsertCommand = oledbCommandBuilder.GetInsertCommand();
oledbDataAdapter.UpdateCommand = oledbCommandBuilder.GetUpdateCommand();
oledbDataAdapter.Update(datatable);
适配器现在知道如何如果发出`dataTable.GetChanges(更新表格
很好,史蒂夫,我错过了当我读取'GetDeleteCommand()'行时OP没有分配命令的事实! –
对不起,但这不起作用:/ – user3105160
什么不起作用?任何错误消息或? – Steve
)'的'观看Window',你得到什么变化? –
这种方法对另一个功能很有用,但不适用于此。谢谢 – user3105160