O'Reilly logo

ADO.NET in a Nutshell by Bill Hamilton, Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required