use*_*160 1 c# database oledb datatable ms-access
如何将 a 保存DataTable到文件中。accdb (Access) 现有一个吗?我使用了以下代码,但它不起作用:
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();
}
Run Code Online (Sandbox Code Playgroud)
变量 dataTable 使用文件的原始内容进行初始化,然后通过添加一行进行修改,现在我必须更新数据库中的表。
我尝试使用以下代码,但这不起作用:(
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);
Run Code Online (Sandbox Code Playgroud)
假设您对数据表进行了一些更改,那么您可以通过这种方式将生成的更新/插入/删除命令传递给适配器
oledbDataAdapter.DeleteCommand = oledbCommandBuilder.GetDeleteCommand();
oledbDataAdapter.InsertCommand = oledbCommandBuilder.GetInsertCommand();
oledbDataAdapter.UpdateCommand = oledbCommandBuilder.GetUpdateCommand();
oledbDataAdapter.Update(datatable);
Run Code Online (Sandbox Code Playgroud)
现在适配器知道如何更新您的表
| 归档时间: |
|
| 查看次数: |
5955 次 |
| 最近记录: |