Database Reference
In-Depth Information
Figure 15-4. Populating a data set
How It Works
After defining a query and opening a connection, you create and initialize a data adapter.
// Create Data Adapter
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
Then you create a data set.
// Create Dataset
DataSet ds = new DataSet();
At this stage, all you have is an empty data set. The key line is where you use the Fill method on the
data adapter to execute the query, retrieve the data, and populate the data set.
// Fill Dataset
da.Fill(ds, "Production.Product");
The Fill method uses a data reader internally to access the table schema and data and then uses
them to populate the data set.
Note that this method isn't just used for filling data sets. It has a number of overloads and can also
be used for filling an individual data table without a data set, if needed.
If you don't provide a name for the table to the Fill method, it will automatically be named TableN ,
where N starts as an empty string (the first table name is simply Table ) and increments every time a new
table is inserted into the data set. It's better practice to explicitly name data tables, but here it doesn't
really matter.
If the same query is run more than once on the data set that already contains data, Fill() updates
the data, skipping the process of redefining the table based on the schema.
 
Search WWH ::




Custom Search