Tuesday, October 14, 2008

How to get value of output parameter of stored procedure in .Net by Enterprise Library

Data Access By - Enterprise Library's Data Access Application Block

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,
@Exists bit output
)

AS

Select @Exists = 0

SELECT ProductName, UnitPrice, QuantityPerUnit
FROM Products
WHERE ProductID = @ProductID

IF @@rowcount > 0
Select @Exists = 1


In Data Access Class's function, You need to write following code.
-----------------------------------------------------------------------------------------------

Dim dr As IDataReader
Dim dbCon As SqlDatabase

Dim sqlCommand As String = "GetProductDetails"

Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
db.AddInParameter(dbCommand, "ProductID", DbType.Int32 , productID)
db.AddOutParameter(dbCommand, "Exists", SqlDbType.Bit, 1)

dr = dbCon.ExecuteReader(dbCom) <- This line returns resultset.

dbCon.ExecuteNonQuery(dbCom) <- This line sets value of output parameter.

bExists = dbCon.GetParameterValue(dbCom, "@Exists")

-vikas

No comments: