您的位置:首页 > 编程语言 > C#

c#中如何使用存储过程的返回值

2007-11-19 11:08 316 查看
存储过程如下:

CREATE PROCEDURE sat_SampleReturn
AS
BEGIN
select * from UserInfo
return 50
END

GO

则在C#中使用如下代码来获得返回参数:

public static int GetSPReturnValue()
{

SqlConnection oConnection = new SqlConnection("");
oConnection.Open();
SqlCommand oCommand = new SqlCommand();
oCommand.Connection = oConnection;
oCommand.CommandType = CommandType.StoredProcedure;
oCommand.CommandText = "sat_SampleReturn";
oCommand.Parameters.Add(new SqlParameter("@Return",SqlDbType.Int));
oCommand.Parameters["@Return"].Direction=ParameterDirection.ReturnValue;
oCommand.ExecuteScalar();
oConnection.Close();
int returnValue =Convert.ToInt32( oCommand.Parameters["@Return"].Value) ;
return returnValue;
}

则会获得returnValue 为50
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: