2

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()// <- My 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

1 Answer 1

4

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()// <- My DataTable
myDA.Fill(MyDT);

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

myDA.Update(MyDT);
Sign up to request clarification or add additional context in comments.

3 Comments

Hi AVD, Can you send me some links for configure those properties, I am newbie, and please forgive for my bad language.Thanks :).
@AVD What does your ` MySqlCommandBuilder cmb=new MySqlCommandBuilder(myDA);`do? You did not seem to call it else where.
@Chenxiao - It automatically generates single-table commands used to reconcile changes made to a DataSet with the associated database. This is an abstract class that can only be inherited. msdn.microsoft.com/en-us/library/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.