IDbReader抛出现有列的异常 [英] IDbReader throwing exceptions for existing columns

查看:157
本文介绍了IDbReader抛出现有列的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从System.Data.Odbc.OdbcDataReader读取值。它的问题是,它根本不工作。当我尝试从现有列(字段)获取值时,它会抛出异常。在我的cae, FieldCount是8 ,但是,例如,如果我调用reader.IsDBNull(4),它抛出。

I'm trying to reader values from a System.Data.Odbc.OdbcDataReader. The problem with it is that it simply doesn't work. When I try to get a value from an existing column (field), it throws an exception. In my cae, FieldCount is 8, but, for instance, a if I invoke reader.IsDBNull(4), it throws.

values列id从0到2,它检索正确的值。

For values column ids from 0 to 2, it retrieves the correct value. But reader[3] to reader[7], an exception is thrown with no information of what happened.

更糟糕的是,这个代码(GetName)也引发了相同的异常!

Even worse, this code (GetName) also throws the same exception!

for (int ordinal = 0; ordinal < reader.FieldCount; ordinal++)
{
    Console.WriteLine("Field {0}: {1}", ordinal, reader.GetName(ordinal));
}

这是在获取现有列的值时抛出的异常:
中的System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle,
RetCode retcode)中的

This is the exception that is thrown when I get the value for an existing column:



中的.Data.Odbc.OdbcDataReader.GetColAttribute(Int32 iColumn,
SQL_DESC v3FieldId,SQL_COLUMN v2FieldId,HANDLER处理程序)System.Data.Odbc.OdbcDataReader.GetSqlType(Int32 i)in
System。 Data.Odbc.OdbcDataReader.GetValue(Int32 i)in
System.Data.Odbc.OdbcDataReader.IsDBNull(Int32 i)in
AisgeXmlVrdb.LogicaNegocio.MappingExtensions.SafeGetString(IDataRecord
reader,Int32 colIndex)in
AisgeXmlVrdb.LogicaNegocio.MetodosComunes.ObtenerSolicitudesExportacionTodas()

in System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode) in System.Data.Odbc.OdbcDataReader.GetColAttribute(Int32 iColumn, SQL_DESC v3FieldId, SQL_COLUMN v2FieldId, HANDLER handler) in System.Data.Odbc.OdbcDataReader.GetSqlType(Int32 i) in System.Data.Odbc.OdbcDataReader.GetValue(Int32 i) in System.Data.Odbc.OdbcDataReader.IsDBNull(Int32 i) in AisgeXmlVrdb.LogicaNegocio.MappingExtensions.SafeGetString(IDataRecord reader, Int32 colIndex) in AisgeXmlVrdb.LogicaNegocio.MetodosComunes.ObtenerSolicitudesExportacionTodas()

我不明白发生了什么。

I don't understand what's happening under the hood.

只是为了添加更多信息,我使用的ODBC驱动程序是Apple的File Maker。

Just to add a bit more information, the ODBC driver I'm using is Apple's File Maker.

更奇怪的是,检查 reader [4] reader [5] [6] ...也会抛出。

What's even more strange is that checking reader[4], reader[5], reader[6]... also throws.

是不是ODBC驱动程序不支持某些东西?

Could it be that the ODBC driver doesn't support something?

推荐答案

如果列索引大于数据中的列总数,我不知道该怎么做。应该有一个例外。

I don't know what should you expect if the column index is greater than total number of columns in data. There should be an exception.

除此之外,您可以缩短代码使用 Convert.ToString ,而不是显式检查 DBNull.Value Convert.ToString 将在 DBNull.Value 的情况下返回空字符串,这是您在方法中执行的操作。所以你的方法可以是:

Other than that, you can shorten your code to use Convert.ToString instead of explicitly checking for DBNull.Value. Convert.ToString will return empty string in case of DBNull.Value, that is what you are doing in your method. So your method could be:

public static string SafeGetString(this IDataRecord reader, int colIndex)
{
    return Convert.ToString(reader[colIndex]);
}

如果由于某种原因要返回空字符串然后你可以显式检查 FieldCount 字段并返回一个空字符串,但这应该避免IMHO。

If for some reason you want to return empty string if the column index is out of bound then you can explicitly check against FieldCount field and returns an empty string, but this should be avoided IMHO.

public static string SafeGetString(this IDataRecord reader, int colIndex)
{
    if (colIndex >= 0 && colIndex < reader.FieldCount)
    {
        return Convert.ToString(reader[colIndex]);
    }
    else
    {
        return "";

    }
}

但我会尽量避免上面的方法,因为它隐藏和修改默认行为,可能最终导致一个难以跟踪的错误。

But I would try to avoid the above approach, as it hides and modify the default behaviour and could end up causing a bug which would be difficult to track.

这篇关于IDbReader抛出现有列的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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