【问题标题】:precision problem on decimal DbParameter十进制 DbParameter 的精度问题
【发布时间】:2009-02-05 00:41:49
【问题描述】:

在 C# 中, 我对 Sql Server 存储过程使用十进制输出 DbParameter。 我创建这样的参数


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);

但是out参数的值总是四舍五入的。

如果我从 Sql Server Management Studio 调用相同的存储过程,我可以把它弄出来 参数正确,精度高

DbParameter 上没有精度或比例属性。 而且我必须在 System.Data.Common 命名空间上使用 DbParameter 来获取数据

我怎样才能以全精度检索十进制值

提前谢谢...

【问题讨论】:

    标签: c# sql ado.net c#-3.0


    【解决方案1】:

    添加到Marc的建议,您可以将代码更改为

    IDbDataParameter prm = comm.CreateParameter();

    其余代码应该可以正常工作。 Precision 和 Scale 属性是 DbParameter 中的“显式接口实现”。

    【讨论】:

    • IDbDataParameter DecimalOut = comm.CreateParameter(); DecimalOut.Direction = ParameterDirection.Output; DecimalOut.ParameterName = "@Price"; DecimalOut.DbType = DbType.Decimal; DecimalOut.Scale = 4; comm.Parameters.Add(DecimalOut);工作正常..谢谢大家..
    【解决方案2】:

    您是否尝试过设置PrecisionScale?请注意,您需要先进行投射。

    【讨论】:

      【解决方案3】:

      首先,你不需要解析字符串来获取十进制,你应该可以直接转换成它:

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

      话虽如此,@Price 参数的精度是多少?我相信 SQL Server 最多允许 35 位的精度,而 Decimal 类只提供最多 28 位的精度,因此如果您的参数超过 28 位,您将无法防止舍入。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-20
        • 1970-01-01
        • 2015-11-26
        相关资源
        最近更新 更多