尝试读取DataReader中缺少的列时处理异常 [英] Handling exception while trying to read column missing in DataReader

查看:70
本文介绍了尝试读取DataReader中缺少的列时处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类DataReaderHelper 从DataReader中读取列值.这些方法可以正常工作,但是会抛出异常,以尝试读取DataReader中不存在的列值.

DataReaderHelper类方法如下:

I have a class DataReaderHelper to read column values from DataReader. These methods work fine, but, throw exception for an attempt to read a column value that doesn’t exists in the DataReader.

The DataReaderHelper class methods are like the followings:

public static string GetString(object data)
{
     return ((data == null) || (data == DBNull.Value)) ? string.Empty : data.ToString();
}



我需要知道如何优雅地处理该问题,这样,如果DataReader不包含所需的列,它就不会引发任何异常(而是返回一些默认值).



I need to know how to handle the problem gracefully, so that, the if the DataReader doesn’t contain the desired column, it doesn''t throw any Exception (Rather, returns some default value or so).

推荐答案

当我们需要为不同的SQL查询共享相同的映射方法时,它们的选择列表可能会有所不同(这是一个非常常见的场景),通常我们会尝试这样做以下内容之一:

将所有列都包括在所有存储过程的选择列表中,即使不是必需的

或者,

when we need to share the same mapping methods for different SQL queries which may have a little differences in their selection list (Which is a very common scenario) and usually we try to do either of the followings:

Include all columns in the selection list of all stored procedures even if not necessary

Or,

Use a try…catch block as follows:

try
{
          string FirstName = DataReaderHelper.GetString(reader["FirstName"]);
}
catch(Exception ex)
{
         //The reader doesn’t have this column in selection
}



或者,

创建不同版本的Mapping方法并为不同的查询创建不同的实体

这些都不是好的方法.

这是一个解决方案


添加以下私有方法,该方法可以检查DataReader
中的列值是否存在



Or,

Create different versions of the Mapping methods and create different entities for different queries

Neither of those are not good approaches.

Here is a solution


Add a following private method which does the trick to check the existence of the column value in the DataReader

private static object GetDataForColumn(string columnName, IDataReader reader)
{
     reader.GetSchemaTable().DefaultView.RowFilter = "ColumnName= ''" + columnName + "''";
     return reader.GetSchemaTable().DefaultView.Count > 0? reader[columnName]:null; 
}



基本上,它使用ColumnName作为过滤器来过滤模式信息,并读取计数以确定DaraReader中是否存在该字段.
现在,您可以使用以下方法:



Basically, it filters the Schema information using the ColumnName as the filter and reads count to determine whether the field exists in the DaraReader.

Now, you can use the method as follows:

public static string GetString(IDataReader reader, string columnName)
{
     object data = GetDataForColumn(columnName, reader);
     return ((data == null) || (data == DBNull.Value)) ? string.Empty : data.ToString();
}



因此,您现在可以使用以下方法:



So, you can use the method as follows now:

string FirstName = DataReaderHelper.GetString(reader,"FirstName");



这样可以很好地处理这种情况:)



This would handle the situation gracefully :)


这篇关于尝试读取DataReader中缺少的列时处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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