我试图让我的DataAdapter从另一个数据库(相同的表但不同的值)更新/插入我的表的新值它应该只复制DB1中的值并在DB2中插入/更新它们。我没有编译错误,DataTables似乎有正确的值。但没有任何内容得到更新或插入。
所以它应该从我的数据库(DB1)填充数据表(dataTable1), 然后设置要修改的所有行并将它们插入DB2 但是,它不会起作用。
DataTable dataTable1 = new DataTable("counters");
string command = "SELECT * FROM counters;"; // same layout and name on both tables
using (MySqlConnection connectionFrom = new MySqlConnection(constringfrom)) // DB1
{
MySqlDataAdapter dataAdapter1 = new MySqlDataAdapter(command, connectionFrom);
dataAdapter1.Fill(dataTable1);
}
using (MySqlConnection ConnectionTo = new MySqlConnection(constringto)) // DB2
{
MySqlDataAdapter dataAdapter2 = new MySqlDataAdapter(command, ConnectionTo);
MySqlCommandBuilder cmb2 = new MySqlCommandBuilder(dataAdapter2);
dataAdapter2.UpdateCommand = cmb2.GetUpdateCommand();
dataAdapter2.InsertCommand = cmb2.GetInsertCommand();
foreach (DataRow r in dataTable1.Rows)
{
r.SetModified(); // just for testing purposes..
}
dataAdapter2.Update(dataTable1); // Correct values but no update to the DB :(
}
Console.WriteLine("Updating..... DONE"); // well... no