[英]Getting null return value from stored procedure
我正在嘗試從存儲過程中獲取字符串返回值,但是我正在獲取null。 存儲過程可以正常運行並返回預期的字符串(我使用SQL Server Profiler檢查了結果),但是我的代碼中得到了空字符串。
public static string GetStringValue(string pCode)
{
String strConnString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand();
string returnValue = "";
cmd.CommandText = "usp_GetStringValue";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = new SqlConnection(strConnString);
cmd.Parameters.AddWithValue("@pcode", pCode);
//cmd.Parameters.AddWithValue("@Result", "");
SqlParameter sqlParam = new SqlParameter("@Result", "");
cmd.Parameters.Add(sqlParam);
sqlParam.Direction = ParameterDirection.Output;
cmd.Connection.Open();
cmd.ExecuteScalar();
//cmd.ExecuteNonQuery();
returnValue = Convert.ToString(cmd.Parameters["@Result"].Value);
cmd.Connection.Close();
return returnValue;
}
這是我的存儲過程
ALTER PROCEDURE [dbo].[usp_GetStringValue]
@pcode Varchar (4),
@Result varchar(5000) OUTPUT
AS
BEGIN
SET @Result=(Select pPath from mytable where pcode = @pcode)
print @Result
END
在添加任何其他參數之前,請嘗試添加@Result,因為它是一個Output參數。 重新排列代碼,如下所示。 過去對我有用。 我可能會為此感到生氣,但這是星期五。
SqlParameter sqlParam = new SqlParameter("@Result", "");
cmd.Parameters.Add(sqlParam);
sqlParam.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.AddWithValue("@pcode", pCode);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.