mysql表没有得到更新

时间:2013-08-10 06:37:21

标签: c# mysql database

我正在将数据从SQL服务器复制到mysql。我从sql server加载一个表,从mysql加载一个表并复制数据。数据将被复制到新表中,但数据库中的表仍为空。提前致谢。这是我的代码 -

private void WriteTable(DataTable table, string tablename)
    {
        long maxid=0;
        MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("select * from " + tablename, mysqlConn);
        MySql.Data.MySqlClient.MySqlDataAdapter adapter = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd);
        DataTable dest = new DataTable();
        adapter.Fill(dest);
        txtMessages.Text += table.Rows.Count.ToString()+"\r\n";
        foreach (DataRow row in table.Rows)
        {
            DataRow newrow = dest.NewRow();
            newrow.BeginEdit();
            foreach (DataColumn col in table.Columns)
            {
                newrow[col.Caption] = row[col.Caption];
            }
            newrow.EndEdit();
            dest.Rows.Add(newrow);
            maxid = long.Parse(row["RowID"].ToString());
            txtMessages.Text += maxid.ToString() + "\r\n";
            SetRowID(tablename, maxid);
        }
        MySql.Data.MySqlClient.MySqlCommandBuilder builder = new MySql.Data.MySqlClient.MySqlCommandBuilder(adapter);
        adapter.DeleteCommand = builder.GetDeleteCommand();
        adapter.InsertCommand = builder.GetInsertCommand();
        adapter.UpdateCommand = builder.GetUpdateCommand();
        dest.AcceptChanges();
        adapter.Update(dest);

    }

1 个答案:

答案 0 :(得分:0)

看看这个SO question提问者和你有同样的问题;通过删除.AcceptChanges();命令解决了这个问题。实际上,您已经告诉缓冲区已经写回了更改,所以当您再创建更多时,它们就不会被提交。