存储过程如下:
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
本文介绍了一种使用C#调用SQL Server存储过程的方法,并展示了如何获取存储过程的返回值。通过示例代码,读者可以了解到具体的实现步骤。

7014

被折叠的 条评论
为什么被折叠?



