IDataRecord.IsDBNull导致出现System.OverflowException(算术溢出)算术、IsDBNull、IDataRecord、OverflowException

2023-09-06 06:41:11 作者:游泳的馄饨

我有一个OdbcDataReader,获取数据库中的数据并返回一组记录。

I have a OdbcDataReader that gets data from a database and returns a set of records.

执行查询的code如下所示:

The code that executes the query looks as follows:

OdbcDataReader reader = command.ExecuteReader();

while (reader.Read())
{
    yield return reader.AsMovexProduct();
}

该方法返回一个自定义类型的一个IEnumerable(MovexProduct )。从IDataRecord到我的自定义类型MovexProduct皈依发生在一个扩展法,看起来像这样(缩写):

The method returns an IEnumerable of a custom type (MovexProduct). The convertion from an IDataRecord to my custom type MovexProduct happens in an extension-method that looks like this (abbrev.):

public static MovexProduct AsMovexProduct(this IDataRecord record)
{
    var movexProduct = new MovexProduct
    {
            ItemNumber = record.GetString(0).Trim(),
            Name = record.GetString(1).Trim(),
            Category = record.GetString(2).Trim(),
            ItemType = record.GetString(3).Trim()
    };

    if (!record.IsDBNull(4)) 
        movexProduct.Status1 = int.Parse(record.GetString(4).Trim());

    // Additional properties with IsDBNull checks follow here.

    return movexProduct;
}

只要我打的如果(!record.IsDBNull(4))我得到异常信息的发生OverflowException算术运算导致溢出。

As soon as I hit the if (!record.IsDBNull(4)) I get an OverflowException with the exception message "Arithmetic operation resulted in an overflow."

堆栈跟踪: 出现System.OverflowException是未处理由用户code   消息=算术运算导致溢出。   来源= System.Data这   堆栈跟踪:        在System.Data.Odbc.OdbcDataReader.GetSqlType(的Int32 I)        在System.Data.Odbc.OdbcDataReader.GetValue(的Int32 I)        在System.Data.Odbc.OdbcDataReader.IsDBNull(的Int32 I)        在JulaAil.DataService.Movex.Data.ExtensionMethods.AsMovexProduct(IDataRecord记录) [...]

StackTrace: System.OverflowException was unhandled by user code Message=Arithmetic operation resulted in an overflow. Source=System.Data StackTrace: at System.Data.Odbc.OdbcDataReader.GetSqlType(Int32 i) at System.Data.Odbc.OdbcDataReader.GetValue(Int32 i) at System.Data.Odbc.OdbcDataReader.IsDBNull(Int32 i) at JulaAil.DataService.Movex.Data.ExtensionMethods.AsMovexProduct(IDataRecord record) [...]

我从来没有遇到过这个问题,我想不通为什么我得到它。我已验证记录存在,它包含的数据,并且我公司提供的指标是正确的。我还要提到的是我得到同样的异常,如果我改变,就算statemnt这样:如果(record.GetString(4)!= NULL)。什么工作是封装的财产分配在尝试{}赶上(NullReferenceException异常){} 块 - 但是,这可能会导致性能损失(不就可以了?)。

I've never encountered this problem before and I cannot figure out why I get it. I have verified that the record exists and that it contains data and that the indexes I provide are correct. I should also mention that I get the same exception if I change the if-statemnt to this: if (record.GetString(4) != null). What does work is encapsulating the property-assignment in a try {} catch (NullReferenceException) {} block - but that can lead to performance-loss (can it not?).

我在运行Visual Studio x64版本和我使用的是64位的ODBC驱动程序。

I am running the x64 version of Visual Studio and I'm using a 64-bit odbc driver.

有没有其他人遇到过吗?任何建议,我怎么能解决/解决这个问题?

Has anyone else come across this? Any suggestions as to how I could solve / get around this issue?

非常感谢!

推荐答案

对于任何一个遇到了同样的问题,我解决这个问题的方法是从ODBC *类切换到自己的OleDb *同行。这当然需要你的数据驱动程序对于OleDb连接的支持。

For any one experiencing the same issue, the way I solved this was to switch from the Odbc* classes to their OleDb* counterparts. This of course demands that your data driver has support for OleDb connections.

 
精彩推荐