2

我有一个如下所示的 DataTable MyDT:

string MyConString = "SERVER=" + sConfig_hostname + ";" +
                "DATABASE=" + sConfig_dbname + ";" +
                "UID=" + sConfig_dbusername + ";" +
                "PASSWORD=" + sConfig_dbpassword + ";Allow Zero Datetime=true;";
            MySqlConnection connection = new MySqlConnection(MyConString);
            string sQuery="Select * from Table";
            connection.Open();
            MySqlDataAdapter myDA = new MySqlDataAdapter(sQuery, connection);
            myDA.SelectCommand.ExecuteNonQuery();
            DataTable MyDT=new DataTable()// <- My DataTable
            myDA.Fill(MyDT);
            connection.Close();
...
...
// do something with MyDT
...
...

用 MyDT 做某事后,我想将它更新到数据库,我该怎么做?我已经在谷歌搜索页面,但几乎通过循环数据表进行更新。有没有办法在没有循环的情况下更新整个数据表?请帮忙!谢谢阅读

4

1 回答 1

4

您需要配置DataAdapter的SelectCommandDeleteCommand和属性。您可以使用填充三个命令对象(选择、删除、更新)。UpdateCommandInsertCommandMySqlCommandBuilder

MySqlConnection connection = new MySqlConnection(MyConString);
string sQuery="Select * from Table";

MySqlDataAdapter myDA = new MySqlDataAdapter(sQuery, connection);
MySqlCommandBuilder cmb = new MySqlCommandBuilder(myDA);

DataTable MyDT = new DataTable()// <- My DataTable
myDA.Fill(MyDT);

//Add new rows or delete/update existing one
//and update the DataTable using 

myDA.Update(MyDT);
于 2012-07-05T02:21:59.587 回答