我在存储过程中使用了SQL Server中的函数并返回RESULT一个小数。为什么我得到这个错误?
在C#中我需要得到它,但我得到一个错误:
“ 型‘system.invaildCastException’的例外“INVALLD CastEXCEPTION是由用户代码unhandeld”出现......”
我的C#代码:
decimal? val = null;
SqlConnection con = new SqlConnection(conStr);
con.Open();
using (con)
{
SqlCommand cmd = new SqlCommand("dbo.spSetOrderDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter tvpParam = cmd.Parameters.AddWithValue("@OrderDetailsItemsTVP", dt);
tvpParam.SqlDbType = SqlDbType.Structured;
// this part of code return the SUM
SqlParameter returnParam = cmd.Parameters.Add("@RESULT", SqlDbType.Decimal);
returnParam.Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
val = (decimal)cmd.Parameters["@RESULT"].Value;--HERE I GET THE EXCEPTION
}
con.Close();
return (decimal)val;
cmd.Parameters [“@ RESULT”。值可能为NULL,你不能将NULL转换为不可为空的小数。 –
你不能带方向的参数返回值是T-SQL中的RETURN状态值的值,该值只能是一个整数 – Steve