Oracle.DataAccess DataRow.Field<&小数GT; InvalidCastException的 [英] Oracle.DataAccess DataRow.Field<decimal> InvalidCastException

查看:833
本文介绍了Oracle.DataAccess DataRow.Field<&小数GT; InvalidCastException的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码是用 System.Data.OracleClient的为ORA数据库连接。我愿与 Oracle.DataAccess 替换此库(因为它已经过时了)。不幸的是,我发现DataRow.Field()抛出 InvalidCastException的。同样的行为与(十进制)x.Rows [0] [COLUME_NAME] 。我没有与 System.Data.OracleClient的这个问题。

In my code I was using System.Data.OracleClient for ora database connection. I would like to replace this library (because it is obsolete) with Oracle.DataAccess. Unfortunately I found that DataRow.Field() throws InvalidCastException. Same behavior is with (decimal)x.Rows[0]["COLUME_NAME"]. I do not have this issue with System.Data.OracleClient.

下面是代码示例

using (var oracleConnection = new OracleConnection(connectionString))
{
    using (var command = new OracleCommand("select * from tr", oracleConnection))
    {
        var result = new DataTable();
        var adapter = new OracleDataAdapter(command);
        adapter.Fill(result);
        Console.WriteLine(result.Rows[0].Field<decimal>("TR_SEQ_NUM"));
        //Console.WriteLine((decimal)result.Rows[0]["TR_SEQ_NUM"]);
    }
}



TR_SEQ_NUM具有 NUMBER(8 ,0)数据类型和充分的例外是:

TR_SEQ_NUM has NUMBER(8,0) datatype and full exception is:

System.InvalidCastException: Specified cast is not valid.
   at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)

代码例如有工作 System.Data.OracleClient的,但不与 Oracle.DataAccess

Code example working with System.Data.OracleClient but not with Oracle.DataAccess

我知道,我可以使用 Convert.ChangeType ,但我不知道是否有某种方式有相同的行为与 System.Data这.OracleClient 。我所有的代码重构将太多的时间昂贵。

I know that I can use Convert.ChangeType but I wonder if there is some way to have same behavior as with System.Data.OracleClient. Refactoring of all of my code will too much time expensive.

推荐答案

看起来您的数据包含null值,你不能转换为空的数据非可空小数类型

Looks your data contains null value and you cannot convert a nullable data to non nullable decimal type

请使用以下说明书读取DB

Please use the below statment to read the nullable decimal column from DB

result.Rows[0].Field<decimal?>("TR_SEQ_NUM")

这篇关于Oracle.DataAccess DataRow.Field&LT;&小数GT; InvalidCastException的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆