IDbReader throwing exceptions for existing columns

随声附和 提交于 2019-12-12 03:35:47

问题


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.

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.

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));
}

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

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.

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

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

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


回答1:


If by "isn't there" you mean exceeds FieldCount then that's the expected behavior for all IDataRecord implementions as spec'd in MSDN

If you want to default the invalid index case to string.Empty:

public static string SafeGetString(this IDataRecord reader, int colIndex)
{
    if (((colIndex >= 0) && (colIndex < reader.FieldCount)) &&
         !reader.IsDBNull(colIndex))
    {
        return reader.GetString(colIndex);
    }

    return string.Empty;
}



回答2:


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.

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]);
}

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.



来源:https://stackoverflow.com/questions/36482532/idbreader-throwing-exceptions-for-existing-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!