Walkthrough: Executing a Command and Accessing Output Parameters

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 retrieve multiple values from a single row by executing a SQL command and retrieving the output parameters. It assumes that the following stored procedure has been created in the database to which the connection string in the code refers.

CREATE PROCEDURE GetProductDetails
@ProductID int,
@ProductName nvarchar(40) OUTPUT,
@UnitPrice money OUTPUT,
@QtyPerUnit nvarchar(20) OUTPUT
AS
SELECT @ProductName = ProductName, 
       @UnitPrice = UnitPrice,
       @QtyPerUnit = QuantityPerUnit
FROM Products 
WHERE ProductID = @ProductID

To reproduce the demonstration

  1. Configure the database. For the necessary steps, see "QuickStart Configuration" in Data Access QuickStart.
  2. 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.
  3. Create the command by adding the following code. It creates a DbCommand used with a stored procedure. In this case, it is GetProductDetails, which takes one input parameter, ProductID, and returns results in two output parameters, ProductName and UnitPrice.
  4. Call ExecuteNonQuery by adding the following code. It passes the DbCommand, indicating that the GetProductsByCategoryDetails stored procedure will populate the output parameters.
  5. Process the results by adding the following code, which places the values stored in the output parameters into a string.

Usage Notes

The ExecuteNonQuery method returns the number of rows affected by the query (typically for Insert, Update, or Delete operations). In the preceding code example, the return value is ignored.