SqlDataReader Contains Column Extension Method

30. July 2015 16:59

It seems I have to look this up a lot.  Maybe if I write it down I’ll remember! This is a slick extension method to determine if a SqlDataReader contains a column. The code is originally from this SO post.

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

About the author

I'm a .NET developer, a husband and a father of three beautiful girls.

Month List