检查列名SqlDataReader对象 [英] Check for column name in a SqlDataReader object

查看:143
本文介绍了检查列名SqlDataReader对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检查,看看是否存在列在 SqlDataReader的对象?在我的数据访问层,我创造建立多个存储过程调用同一个对象的方法。一的存储过程具有未使用的其他的存储过程的附加列。欲修改,以适应对每个场景的方法

How do I check to see if a column exists in a SqlDataReader object? In my data access layer, I have create a method that builds the same object for multiple stored procedures calls. One of the stored procedures has an additional column that is not used by the other stored procedures. I want to modified the method to accommodate for every scenario.

我的应用程序是用C#。

My application is written in C#.

推荐答案

在接受的答案,使用异常用于控制逻辑被认为是不好的做法,并具有性能开销。

In the accepted answer, using Exceptions for control logic is considered bad practice and has performance costs.

在田野循环可以有一个小的性能损失,如果你经常使用它,你可能需要考虑缓存结果

Looping through the fields can have a small performance hit if you use it a lot and you may want to consider caching the results

更合适的方式来做到这一点:

The more appropriate way to do this is:

public static class DataRecordExtensions
{
    public static bool HasColumn(this IDataRecord dr, string columnName)
    {
        for (int i=0; i < dr.FieldCount; i++)
        {
            if (dr.GetName(i).Equals(columnName, StringComparison.InvariantCultureIgnoreCase))
                return true;
        }
        return false;
    }
}

这篇关于检查列名SqlDataReader对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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