使用 TableAdapter 将行插入 Access 2003 DB
我是 TableAdapters 的新手,我不确定发生了什么。本质上,我试图使用生成的数据集将数据从一个数据库导出到具有不同架构的另一个数据库。当我单步执行代码时,就填充目标数据库的行而言,一切似乎都工作正常。但是,当我尝试将一行添加到目标数据库时,该行似乎没有被插入。你们有什么想法吗?我已将添加到项目中的数据库设置为不复制到输出目录...所以我在网上看到的建议似乎不起作用。
OleDbConnection oleDbConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Database\database.mdb;");
SomeTableAdapter tableAdapter = new SomeTableAdapter();
tableAdapter.Connection = oleDbConnection;
tableAdapter.Connection.Open();
SomeDataSet.SomeDataTable dataTable = tableAdapter.GetData();
SomeDataSet.SomeDataRow dataRow = null;
// Do some checks on the existing rows
// Creation of new row is necessary
if (dataRow == null)
dataRow = dataTable.NewSomeRow();
// Populate row fields
dataTable.AddSomeRow(dataRow);
dataTable.AcceptChanges();
}
else
{
// Update exiting row
}
tableAdapter.Update(dataTable);
tableAdapter.Connection.Close();
I'm new to using TableAdapters and I am unsure of what is going on. Essentially, I am trying to export data from one database to another database with a different schema using a generated dataset. Everything seems to be working fine, in terms of populating the row of the target database, when I step through the code. However, when I try to have a row added to the target database, the row does not seem to get inserted. Do you guys have any ideas? I have set the database that was added to the project set to not copy to the output directory...so the suggestions I saw on the web didn't seem to work.
OleDbConnection oleDbConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Database\database.mdb;");
SomeTableAdapter tableAdapter = new SomeTableAdapter();
tableAdapter.Connection = oleDbConnection;
tableAdapter.Connection.Open();
SomeDataSet.SomeDataTable dataTable = tableAdapter.GetData();
SomeDataSet.SomeDataRow dataRow = null;
// Do some checks on the existing rows
// Creation of new row is necessary
if (dataRow == null)
dataRow = dataTable.NewSomeRow();
// Populate row fields
dataTable.AddSomeRow(dataRow);
dataTable.AcceptChanges();
}
else
{
// Update exiting row
}
tableAdapter.Update(dataTable);
tableAdapter.Connection.Close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用 AcceptChanges() 等于搬起石头砸自己的脚。它将对数据集的所有修改标记为“已提交”,因此下一次更新会忽略它们。
通常,您不应直接调用 AcceptChanges,除非在高级场景中。
You are shooting yourself in the foot by calling AcceptChanges(). It marks all the modifications to the dataset as 'committed' so the next Update simply ignores them.
You generally shouldn't ever call AcceptChanges directly, except in advanced scenarios.