I have a DataTable MyDT like below:
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()//
myDA.Fill(MyDT);
connection.Close();
...
...
// do something with MyDT
...
...
After do something with MyDT , I want to update it to the DataBase, how can I do it ?I have searched google for pages but almost update by looping the datatable. is there a way to update the whole datatable without loop?Please help! Thanks for reading
解决方案
You need to configure SelectCommand, DeleteCommand, UpdateCommand and InsertCommand properties of DataAdapter. You may use MySqlCommandBuilder to populate three command objects (Select, Delete, Update).
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()//
myDA.Fill(MyDT);
//Add new rows or delete/update existing one
//and update the DataTable using
myDA.Update(MyDT);