Retrieving Data from the Data Source
The Fill( )
method
of the DataAdapter
retrieves data from the data
source into a DataSet
or a
DataTable
. When the Fill( )
method for the data adapter is called, the select statement defined
in the SelectCommand
is executed against the data
source and retrieved into a DataSet
or
DataTable
. In addition to retrieving data, the
Fill( )
method retrieves schema information for
columns that don’t exist. This schema that it
retrieves from the data source is limited to the name and data type
of the column. If more schema information is required, the
FillSchema( )
method, described later in this
chapter, can be used. The following example shows how to use the
Fill( )
method to retrieve data from the
Orders
table in the Northwind database:
// connection string and the select statement String connString = "Data Source=(local);Integrated security=SSPI;" + "Initial Catalog=Northwind;"; String selectSQL = "SELECT * FROM Orders"; SqlDataAdapter da = new SqlDataAdapter(selectSQL, connString); // create a new DataSet to receive the data DataSet ds = new DataSet(); // read all of the data from the orders table and loads it into the // Orders table in the DataSet da.Fill(ds, "Orders");
A DataTable
can also be filled similarly:
// ... code to create the data adapter, as above // create the DataTable to retrieve the data DataTable dt = new DataTable("Orders"); // use the data adapter to load the data into the table Orders da.Fill(dt);
Notice that a connection object ...
Get ADO.NET in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.