NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Updating the Database with a DataSetCommand and the DataSet

It's mentioned in the previous section that the ADO and SQL DataSetCommands contains four Command objects. The first is the Select command, already covered. Now we'll cover the Insert, Update and Delete commands.

After making changes in the DataSet, you'll want to push those changes back to the database. You do this by calling the Update method of the DataSetCommand. The Update method, like the FillDataSet method takes an instance of a DataSet and the source table name. The DataSet instance is the DataSet that contains the changes that you've made, while the source table name identifies the DataTable from which to pull the changes.

When the FillDataSet method is called, it analyzes the changes that have been made and executes the appropriate command. The DataSet takes the changes in the order they are presented. So, if you want to have all of the updates done prior to deletes, you will need to filter them before sending them down to the Update method.

[VB]

Dim workDS As DataSet = New DataSet("myDataSet")
Dim workDSCMD As ADODataSetCommand = New ADODataSetCommand _
("SELECT * FROM Customers", _
"Provider=SQLOLEDB.1;Initial Catalog=Northwind;" & _
"Data Source=MyServer;User ID=sa;")
workDSCMD.FillDataSet(workDS, "Customers")
' Make some changes to the customer data in the DataSet.
workDSCMD.Update(workDS, "Customers")

[C#]

DataSet workDS = new DataSet("mydataset");
ADODataSetCommand workDSCMD = new ADODataSetCommand("SELECT * FROM 
Customers", "Provider=SQLOLEDB.1;Initial Catalog=Northwind;
Data Source=MyServer;User ID=sa;");
workDSCMD.FillDataSet(workDS, "Customers");
// Make some changes to the customer data in the DataSet.
workDSCMD.Update(workDS, "Customers");

This is a good place to recall how "removing" differs from "deleting" (see Removing Versus Deleting Data from a Table). If you actually "remove" the row from the collection, the DataSetCommand won't see that row when you update the DataSet. Thus it won't execute any command for that row. However, if the row is marked as "deleted," it will the Delete command will be invoked.