简体   繁体   中英

mysql table is not getting updated

I am copying data from SQL server to mysql. I load a table from sql server and a table from mysql and copy the data over. The data is getting copied into the new table but the tables in database remains empty. thanks in advance. Here is my code -

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);

    }

Take a look over at this SO question the asker had the same problem as you; it was resolved by removing the .AcceptChanges(); command. In effect you have already told the buffer that changes have been written back, so when you then make more they do not get committed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM