1、首先建立一个具有返回值的存储过程,作为示例,我就简单的建一个存储过程,如下:
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
(
@id int
)
as
declare @flag int
select * from person where id = @id
if @@rowcount > 0
set @flag = 1
else
set @flag = 0
return @flag
我们要在程序中获得这个返回值的方法如下:
[TestMethod]
public void TestReturnValue()
{
Database db = DatabaseFactory.CreateDatabase();
DbCommand dbcomm = db.GetStoredProcCommand("test");
db.AddInParameter(dbcomm, "@id", DbType.Int32,1);
//关键在这里,添加一个参数,类型为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);
}
public void TestReturnValue()
{
Database db = DatabaseFactory.CreateDatabase();
DbCommand dbcomm = db.GetStoredProcCommand("test");
db.AddInParameter(dbcomm, "@id", DbType.Int32,1);
//关键在这里,添加一个参数,类型为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);
}
通过上面的代码我们就能够在程序中获得存储过程的返回值了。
以前写的文章可能还有很多地方没说到,希望能有更多的朋友提意见,谢谢!