Walkthrough: Using a DataSet to Retrieve Multiple Rows
Retired Content |
---|
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. |
The latest Enterprise Library information can be found at the Enterprise Library site. |
This walkthrough demonstrates how to use a DataSet to retrieve multiple rows of data. It assumes that the following stored procedure has been created in the database to which the connection string in the code refers.
CREATE Procedure GetProductsByCategory
(
@CategoryID int
)
AS
SELECT ProductID, ProductName, CategoryID, UnitPrice, LastUpdate
FROM Products
WHERE CategoryID = @CategoryID
To reproduce the demonstration
- Configure the database. For the necessary steps, see "QuickStart Configuration" in Data Access QuickStart.
- Create a method named GetProductsInCategory that accepts a category identifier as an integer parameter and returns a DataSet. Add the following code to this method.
- Create the database (when you are not using the Unity Integration approach). The following code uses the factory to create a Database object that has the default configuration.
- Create the command by adding the following code. This code creates the DbCommand object used with a stored procedure. In this case, the code calls GetProductsByCategory. The DbCommand takes one input parameter, CategoryID.
- Declare the DataSet, which will be created by the ExecuteDataSet call.
- Call ExecuteDataSet by adding the following code. ExecuteDataSet passes the DbCommand object, which indicates that GetProductsByCategory will populate the DataSet.
- Return the results to the user interface code. The user interface can process the results by adding the following code, which binds the returned DataSet to a DataGrid.