【问题标题】:Why does OleDb ExecuteScalar method return Decimal when querying for COUNT?为什么 OleDb ExecuteScalar 方法在查询 COUNT 时返回 Decimal?
【发布时间】:2011-12-16 19:18:54
【问题描述】:

我正在使用 OleDb 查询 Oracle 10g 数据库。

我的查询非常简单,只需从给定表中获取 COUNT 条记录。

在使用 ExecuteScalar 时,我在尝试将结果值转换为 Int 时遇到了转换错误。检查类型,结果是 System.Decimal。

现在,我已经看到其他用户在使用 ID 时遇到了类似的问题,但在这种情况下,我只是返回了一个预期为整数的计数,那么我为什么还要强制转换它呢?

SQL 如下。

 _sqlText = @" select count(*) as num_records from ce_quote_ext 
        where ce_quoteid in (select ce_quoteid from ce_quote where opportunityid in 
        (select opportunityid from ce_quote where ce_quoteid = '?' )) and custsign = 'T' ";

【问题讨论】:

  • 查看使用的 oracle 驱动程序规范。它可以为数字指定超类型,为紧凑性指定其他类型。

标签: c# sql oledb


【解决方案1】:

返回小数点的确实是 Oracle OleDb 库。这是正确的行为。有关 Oracle 文档,请参阅 here。在 Oracle 文档的示例中,他们将 COUNT() 函数的返回值转换为小数。所以,它是设计出来的。

// C#
...
CmdObj.CommandText = "select count(*) from emp";
decimal count = (decimal) CmdObj.ExecuteScalar();
...

【讨论】:

  • 这就是让我绊倒的原因。 ExecuteScalar 的 MSDN 文档显示未装箱为 Int32 的返回值。没有对提供程序的具体引用,它对返回类型有潜在影响。 msdn.microsoft.com/en-us/library/… 话虽如此,我的提供者是一个自定义的 OleDb 数据提供者。没有迹象表明我正在使用 Oracle 数据库,并且它没有使用 Oracle .NET 程序集。因此,虽然 Oracle 文档以一种方式描述了它,但我期待另一种方式。想法?
  • 想法...文档很烂? :) 说真的,到底是什么问题?您不想投射对象?
  • 现在不是问题,只是好奇。
猜你喜欢
  • 2011-04-04
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 2017-12-12
  • 2013-02-25
相关资源
最近更新 更多