我正在使用 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 OleDb 库。这是正确的行为。参见 here对于 Oracle 文档。在 Oracle 文档的示例中,他们将 COUNT() 函数的返回值转换为小数。所以,它是有意设计的。
// C#
...
CmdObj.CommandText = "select count(*) from emp";
decimal count = (decimal) CmdObj.ExecuteScalar();
...
关于c# - 为什么 OleDb ExecuteScalar 方法在查询 COUNT 时返回 Decimal?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8539072/