存储过程返回值无法正常工作

时间:2015-07-22 23:33:59

标签: c# asp.net stored-procedures

似乎无法让它发挥作用。

SqlConnection sqlConnection1 = new SqlConnection(WebConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
SqlCommand cmd = new SqlCommand();
sqlConnection1.Open();

cmd.CommandText = "sp_wth";
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter retval = cmd.Parameters.Add("@b", SqlDbType.VarChar);
retval.Direction = ParameterDirection.ReturnValue;
cmd.Connection = sqlConnection1;
cmd.ExecuteNonQuery();

string returnvalue = (string)cmd.Parameters["@b"].Value;
lblStatusMessage.Text = returnvalue;

2 个答案:

答案 0 :(得分:1)

SqlConnection con = new SqlConnection("connection string");
con.Open();
SqlCommand cmd = new SqlCommand("exec sp_wth", con);
cmd.Parameters.Add(new SqlParameter("@b", value));
cmd.ExecuteNonQuery();
con.Close();

答案 1 :(得分:0)

检查ParameterDirection。是ReturnValue还是Output

ParameterDirection.Output :参数是输出参数。

示例:

CREATE PROCEDURE TestSP
@b varchar(50) OUTPUT,
...

ParameterDirection.ReturnValue :该参数表示来自诸如存储过程,内置函数或用户定义函数之类的操作的返回值。

实施例

CREATE PROCEDURE TestSP
@b varchar(50)
...
return @b