Today, a friend asked me how to get the return value of the stored procedure in the Data Access Application Block. I discovered that this issue was not mentioned in the articles I wrote before. Now I would like to add that the specific solution is as follows:
1. First create a stored procedure with a return value. As an example, I will simply create a stored procedure as follows:
create proc test
(
@id int
)
as
declare @flag int
select * from person where id=@id
if @@rowcount > 0
set @flag=1
else
set @flag=0
return @flag
The method we use to obtain this return value in the program is as follows:
[TestMethod]
public void TestReturnValue()
{
Database db = DatabaseFactory.CreateDatabase();
DbCommand dbcomm = db.GetStoredProcCommand("test");
db.AddInParameter(dbcomm, "@id", DbType.Int32,1);
//The key is here, add a parameter of type ReturnValue
db.AddParameter(dbcomm, "@RETURN_VALUE", DbType.String, ParameterDirection.ReturnValue, "", DataRowVersion.Current, null);
db.ExecuteNonQuery(dbcomm);
int testvalue = (int)dbcomm.Parameters["@RETURN_VALUE"].Value;
Assert.AreEqual(testvalue, 1);
}
Through the above code, we can obtain the return value of the stored procedure in the program.
There may be many things that I haven't mentioned in the articles I wrote before. I hope more friends can give me their opinions. Thank you!