使用 Ado.NET 将数据从 Database1 Mysql TableA 复制到 Database2 Mysql TableA
使用 Entity Framework 将数据从一个数据库传输到另一个数据库似乎并不简单。所以我再次尝试使用数据表和dataadapters:
MySqlDataAdapter dataAdapter = new MySqlDataAdapter();
MySqlCommand command = connection.CreateCommand();
string query2 = "SELECT * FROM `" + targetTable + "`";
command.CommandText = query2;
dataAdapter.SelectCommand = command;
DataTable tableToUpdate = new DataTable("myTable");
dataAdapter.FillSchema(tableToUpdate, SchemaType.Source);
dataAdapter.Fill(tableToUpdate);
// an den DataAdapter übergeben
MySqlCommandBuilder myComBuilder = new MySqlCommandBuilder(dataAdapter);
dataAdapter.UpdateCommand = myComBuilder.GetUpdateCommand();
dataAdapter.InsertCommand = myComBuilder.GetInsertCommand();
tableToUpdate.Merge(sourceDT,false);
int changeCount = dataAdapter.Update(tableToUpdate);s
DataTable changes = tableToUpdate.GetChanges();
更新和插入命令似乎是正确的,但数据库中没有任何更新?
编辑: 问题似乎出在数据表的合并上。不添加新列。添加了新行,但行状态未更改。
The transfer of data from one database to another with the Entitiy Framework doesn't seem to be simple. So i have another try with datatables & dataadapters:
MySqlDataAdapter dataAdapter = new MySqlDataAdapter();
MySqlCommand command = connection.CreateCommand();
string query2 = "SELECT * FROM `" + targetTable + "`";
command.CommandText = query2;
dataAdapter.SelectCommand = command;
DataTable tableToUpdate = new DataTable("myTable");
dataAdapter.FillSchema(tableToUpdate, SchemaType.Source);
dataAdapter.Fill(tableToUpdate);
// an den DataAdapter übergeben
MySqlCommandBuilder myComBuilder = new MySqlCommandBuilder(dataAdapter);
dataAdapter.UpdateCommand = myComBuilder.GetUpdateCommand();
dataAdapter.InsertCommand = myComBuilder.GetInsertCommand();
tableToUpdate.Merge(sourceDT,false);
int changeCount = dataAdapter.Update(tableToUpdate);s
DataTable changes = tableToUpdate.GetChanges();
the update and insert commands seem to be right, but nothing is updated in the database?
edit:
The problem seems to be in the merging of the dataTables. New columns aren't added. New Rows are added but the Rowstate is Unchanged.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论