How do I get the result of my command and fill it to the dataset?
DataSet is a local and offline container of the data. The DataSet object is created simply like
C# Version
DataSet ds = new DataSet();
VB.Net Version
Dim ds As New DataSet()
Now we need to fill the DataSet with the result of the query. We will use the dataAdapter object for this purpose and call its Fill() method. This is the step where data adapter connects to the physical database and fetch the result of the query.
C# Version
dataAdapter.Fill(ds, "prog");
VB.Net Version
da.Fill(ds, "prog")
Here we have called the Fill() method of dataAdapter object. We have supplied it the dataset to fill and the name of the table (DataTable) in which the result of query is filled.
This is all we needed to connect and fetch the data from the database. Now the result of query is stored in the dataset object in the prog table which is an instance of DataTable. We can get a reference to this table by using the indexer property of dataset object’s Tables collection
C# Version
DataTable dataTable = ds.Tables["prog"];
VB.Net Version
Dim dataTable As DataTable
dt = ds.Tables("prog")
The indexer we have used takes the name of the table in dataset and returns the corresponding DataTable object. Now we can use the tables Rows and Columns collection to access the data in the table.
Back