简体   繁体   English

如何在c#中使用SQL Server存储过程数值参数

[英]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.

相关问题 如何获取SQL Server存储过程输出参数C# - How to get SQL Server stored procedure output parameter C# C#:如何使用SQL Server存储过程的参数? - C# : how to use parameters for SQL Server stored procedure? 通过存储过程将SQL Server输出参数返回到C# - Returning SQL Server Output parameter to C# by Stored Procedure 在C#中读取输出sql server存储过程参数 - Reading output sql server stored procedure parameter in C# 在C#中访问SQL Server存储过程输出参数 - Accessing SQL Server stored procedure output parameter in C# 如何将SQL Server存储过程参数类型转换为等效的C#类型? - How would I get SQL Server stored procedure parameter types to their equivalent C# types? 如何将可为空的guid声明为带有SQL Server的C#CLR存储过程的可选参数 - How to declare a nullable guid as an optional parameter for a C# CLR stored procedure with SQL Server 将参数传递给c#中的sql存储过程 - Passing a parameter to an sql stored procedure in c# 从C#传递参数到存储过程:哪个SQLDbType用于数字(18,0) - Passing parameter to stored procedure from C#: Which SQLDbType to use for numeric(18, 0) 如何在 C# 中的存储过程中使用输出参数 - How To Use Output Parameter In Stored Procedure In C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM