Search code examples
.netdatasetdatabase-connectionsqlcommandbuilder

How do I update a database using a DataSet?


I am trying to update my database with the contents of a DataSet, but currently am unable to do so with the following code:

string s = "Select number,name from table where id = 5 and num = 20";

SqlDataAdapter adapter = new SqlDataAdapter(s, con);
adapter.Fill(dset, "ABC");

SqlCommandBuilder sT = new SqlCommandBuilder(adapter);
adapter.Update(dset,"ABC");

This code is not updating the ABC table in the database.


Solution

  • I've found (with the related OleDbCommandBuilder) that despite what the MSDN documention will tell you, you need to manually set the adapter's InsertCommand, UpdateCommand, and DeleteCommand to be able to use those.

            // a is the adapter
            // cb is the commandbuilder
            a.InsertCommand = cb.GetInsertCommand();
            a.DeleteCommand = cb.GetDeleteCommand();
            a.UpdateCommand = cb.GetUpdateCommand();