[翻译]  Using SqlDBType.Decimal in Prepared Statement C#

[CHINESE]  在准备语句C#中使用SqlDBType.Decimal


am using a Prepared Statement in C#.

我在C#中使用准备好的声明。

 SqlCommand inscommand = new SqlCommand(supInsert, connection);
 inscommand.Parameters.Add("@ordQty", SqlDbType.Decimal,18);
 inscommand.Prepare();
 u = inscommand.ExecuteNonQuery();

The above code throws below Exception:

上面的代码抛出异常:

SqlCommand.Prepare method requires parameters of type 'Decimal' have an explicitly set Precision and Scale.

SqlCommand.Prepare方法要求“Decimal”类型的参数具有显式设置的Precision和Scale。

EDIT: How to avoid this Exception

编辑:如何避免此异常

4 个解决方案

#1


28  

The following would set a Decimal with Precision 18 and Scale 8 (Decimal (18,8))

以下将设置精度18和刻度8的十进制(十进制(18,8))

SqlCommand insertCommand= new SqlCommand(supInsert, connection);
insertCommand.Parameters.Add("@ordQty", SqlDbType.Decimal,18);

insertCommand.Parameters["@ordQty"].Precision = 18;
insertCommand.Parameters["@ordQty"].Scale = 8;

insertCommand.Prepare();
u = insertCommand.ExecuteNonQuery();

#2


6  

As the exception pointed out, you have to explicitly set the SqlParameter.Precision and SqlParameter.Scale properties in order to use the decimal type as a parameter.

正如异常所指出的,您必须显式设置SqlParameter.Precision和SqlParameter.Scale属性才能使用小数类型作为参数。

Let's say your SQL field is of type decimal(18,8). The way to do this inline is to use brace-initialization for your SqlParameter while adding it to the SqlParameterCollection, as follows:

假设您的SQL字段是十进制类型(18,8)。内联方法是在将SqlParameter添加到SqlParameterCollection时对SqlParameter使用大括号初始化,如下所示:

cmd.Parameters.Add(new SqlParameter("@ordGty", SqlDbType.Decimal) {
    Precision = 18, Scale = 8 });

You can also do

你也可以这样做

cmd.Parameters.Add(new SqlParameter("@ordGty", SqlDbType.Decimal) {
    Precision = 18, Scale = 8}).Value = 0.4m; // or whatever

to add the value, if you need one. You could even do

如果需要,可以添加值。你甚至可以这样做

cmd.Parameters.Add(new SqlParameter("@ordGty", SqlDbType.Decimal) {
    Precision = 18, Scale = 8, Value = 0.4m /* or whatever */});

if you prefer. Brace initialization is really powerful.

如果你更喜欢。 Brace初始化非常强大。

Side note: I realize that this is an old question, but I think this form is much more readable than creating the object while adding it to the list and then setting the scale and precision. For Posterity! (since this is a high-listing google search result)

旁注:我意识到这是一个老问题,但我认为这个形式比创建对象更可读,同时将其添加到列表中,然后设置比例和精度。为了后人! (因为这是一个高级列表谷歌搜索结果)

#3


4  

try this:

尝试这个:

SqlParameter parameter = new SqlParameter("@ordQty", SqlDbType.Decimal);
parameter.Precision = 18;
parameter.Scale = 0;
parameter.Value = YOURVALUEHERE;
inscommand.Parameters.Add(parameter);

#4


3  

You will have to explicitly define precision and scale for this parameter.

您必须明确定义此参数的精度和比例。

SqlParameter ordQty = cmd.Parameters.Add("@ordQty", SqlDbType.Decimal);
ordQty.Precision = x; //Replace x with what you expect in Sql Sp
ordQty.Scale = y; //Replace y with what you expect in Sql Sp
ordQty.Value = 18; //Set value here
inscommand.Parameters.Add(ordQty);

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号