java isdbnull,IDataRecord.IsDBNull导致出现System.OverflowException(算术溢出)

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

The code that executes the query looks as follows:

OdbcDataReader reader = command.ExecuteReader();

while (reader.Read())

{

yield return reader.AsMovexProduct();

}

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;

}

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

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) [...]

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?).

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?

Many thanks!

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值