SQLCommand.Parameters.Add - How to give decimal value size?

China☆狼群 提交于 2020-12-08 05:33:05

问题


How would you specify this:

Decimal(18,2)

In this:

SqlComm.Parameters.Add("@myValue", SqlDbType.Decimal, 0, "myValue");

Currently I have defined precision = 2 from the design side properties. I'm just curious as to how to accomplish this from the code. Thanks


回答1:


There's not an overload of Add that lets you set the decimal precision inline, so you either need to create a SQlParameter object and add it to the collection:

SqlParameter param = new SqlParameter("@myValue", SqlDbType.Decimal);
param.SourceColumn = "myValue";
param.Precision = 18;
param.Scale = 2;
SqlComm.Parameters.Add(param);

or "find" the parameter after adding it:

SqlComm.Parameters.Add("@myValue", SqlDbType.Decimal, 0, "myValue");
SqlParameter param = SqlComm.Parameters["@myValue"];
param.Precision = 18;
param.Scale = 2;


来源:https://stackoverflow.com/questions/31391922/sqlcommand-parameters-add-how-to-give-decimal-value-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!