Precision Decimal DbParameter Task

In C #, I am using decimal DbParameter for the Sql server stored procedure. I create such a parameter


DbParameter prm = comm.CreateParameter(); prm.ParameterName = "@Price"; prm.DbType = DbType.Decimal; prm.Direction = ParameterDirection.Output; comm.Parameters.Add(prm); //and set the value of comm.Parameters["@Price"] to a variable, decimal.TryParse(comm.Parameters["@Price"].Value.ToString(), out this.ProdPrice); 

But the value of the out parameter is always rounded.

If I call the same stored procedure from Sql Server Management Studio, I can get its parameter correctly with it accuracy

There are no precision or scaling properties in DbParameter. And I have to use DbParameter in the System.Data.Common namespace to retrieve the data

How can I get the decimal value with full precision

Thanks in advance...

+4
source share
3 answers

By adding to the Marc proposal, you can change the code to

IDbDataParameter prm = comm.CreateParameter();

The rest of the code should work fine. The precision and scaling properties are an "explicit interface implementation" in DbParameter.

+4
source

Have you tried setting Precision and Scale ? Please note that you need to do the first.

+2
source

Firstly, you do not need to parse the string to get the decimal value, you should be able to use it directly:

 this.ProdPrice = (Decimal) comm.Parameters["@Price"].Value; 

As the saying goes, what is the accuracy of the @Price parameter? I believe that SQL Server allows accuracy of up to 35 places, and the Decimal class provides accuracy of up to 28 places, so if your parameter has more than 28 places, you cannot prevent rounding.

+1
source

All Articles