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!

Creating a Simple DataSetCommand

The basic function of a data set command is to pull data out of a data store and push it into a DataTable within the DataSet. To accomplish this, the data set command requires two pieces of information: the connection and the select command to execute.

In the following example we build a ADODataSetCommand by providing the select command text and an OLEDB Connection string:

[VB]

Dim workDSCMD As ADODataSetCommand
WorkDSCMD = New ADODataSetCommand _
("Select * from Customers", _
"Provider=SQLOLEDB.1;Initial Catalog=Northwind;" & _
"Data Source=MyServer;User ID=sa;")

[C#]

ADODataSetCommand workDSCMD = new ADODataSetCommand("Select * from 
Customers", "Provider=SQLOLEDB.1;Initial Catalog=Northwind;
Data Source=MyServer;User ID=sa;");

The above code (1) creates a new ADOConnection object based on the connection string, (2) creates a new ADOCommand object based on the select statement and the newly created connection, and (3) assigns the new ADOCommand object to the Select command of workDSCMD.

Another way of doing this would be to create instances of each of the objects and then construct the data set command through it's properties.

[VB]

Dim workDSCMD As ADODataSetCommand
Dim workCMD As ADOCommand
Dim workConn As ADOConnection

workConn = New ADOConnection _
("Provider=SQLOLEDB.1;Initial Catalog=Northwind;" & _
"Data Source=MyServer;User ID=sa;")

workCMD = New ADOCommand("SELECT * FROM Customers", workConn)
workDSCMD = new ADODataSetCommand()
workDSCMD.SelectCommand = workCMD

[C#]

ADODataSetCommand workDSCMD;
ADOCommand workCMD;
ADOConnection workConn;

workConn = New ADOConnection("Provider=SQLOLEDB.1;Initial Catalog=Northwind; 
      Data Source=MyServer;User ID=sa;");
workCMD = New ADOCommand("SELECT * FROM Customers", workConn);
workDSCMD = New ADODataSetCommand()
workDSCMD.SelectCommand = workCMD

We now have a DataSetCommand that can be used to fill a DataTable contained by a DataSet, but here follows a discussion detailing how to fill the table, and what actually happens when you do.

How you do accomplish this is easy: both the ADO and SQL DataSetCommand objects feature the FillDataSet method which takes two parameters, a DataSet instance and a source table name. The DataSet instance represents the DataSet to fill, and the source table name identifies the table inside of the DataSet.

In the following example, a new instance of a DataSet is created, and passed as an argument when invoking the FillDataSet 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")

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");

After invoking the FillDataSet method, the result is a populated table named "Customers" in the DataSet. The question is, how did the Customers table get into the DataSet since we didn't actually create it the prior to executing the FillDataSet method.

With both the SQL and ADO DataSetCommand objects, if you don't change the default settings, any schema (table/column/primary key definitions) from the data store that don't exist inside of the DataSet will be created automatically. So, in the example above, none of the schema from the Customers table existed previously.

Therefore, if you build the Customers table prior to executing the FillDataSet method, the DataSetCommand will simply fill the existing table.

Because there is no physical link between the DataSet and the DataSetCommand, the DataSetCommand can be used to fill any number of DataSet instances. Simply create the DataSet instance and pass it as an argument in the FillDataSet method.