Monday, August 25, 2008

Enterprise Library data block and return values

I've seen various posts discussing how to get return values from calls via the Enterprise Library data block, but many seemed convoluted. I didn't need multiple output parameters, just the one integer. Here's what I did to capture the return value from a stored procedure call:

Within the stored procedure
CREATE PROCEDURE [dbo].[GetReturnValue]
AS
RETURN 1

Enterprise Library call
Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetStoredProcCommand("dbo.GetReturnValue");
db.AddParameter(cmd, "return_value", DbType.Int32, ParameterDirection.ReturnValue, null, DataRowVersion.Default, null);

db.ExecuteNonQuery(cmd);

int myReturnValue = Convert.ToInt32(db.GetParameterValue(cmd, "return_value"));
HTH.

1 comment: