[英]How to use SQL Server stored procedure numeric parameter in c#
I'm using the following SQL Server stored procedure in one of my C# projects. 我在我的一个C#项目中使用以下SQL Server存储过程。
create procedure [dbo].[TestRecordSelect]
@OMarks numeric(3,1) output
as
begin
select @OMarks = isnull(sum(cast(OMarks as numeric(3,1))),0) from Marks
end
and I'm getting the desired result ie 24.9 when executing the procedure in SQL Server. 当我在SQL Server中执行该过程时,我得到了所需的结果,即24.9。 But when I'm using the same procedure in my C# project and calling the
@OMarks
as follows 但是当我在我的C#项目中使用相同的过程
@OMarks
如下方式调用@OMarks
cmd.Parameters.Add("@OMarks", SqlDbType.Decimal).Direction = ParameterDirection.Output;
tbomarks.Text = cmd.Parameters["@OMarks"].Value.ToString();
I'm getting 25 instead of 24.9 that is the rounded up data. 我得到25而不是24.9这是四舍五入的数据。
My problem is I don't want to get 25. I want to get 24.9 in tbomarks 我的问题是我不想得到25.我希望得到24.9的tbomarks
SqlParameter param = new SqlParameter("@OMarks", SqlDbType.Decimal);
param.Direction = ParameterDirection.Output;
param.Precision = 3;
param.Scale = 1;
cmd.Parameters.Add(param);
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.scale.aspx http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.scale.aspx
In your C#, you haven't specified the Scale (or Precision) of your parameter, which means you get zero decimal places. 在C#中,您没有指定参数的Scale (或Precision),这意味着您将获得零小数位。
Data may be truncated if the Scale property is not explicitly specified and the data on the server does not fit in scale 0 (the default)
如果未明确指定Scale属性且服务器上的数据不适合缩放0(默认值),则可能会截断数据
Precision and Scale are required for output parameters
输出参数需要精度和比例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.