ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ColumnExists

Returns true if the column exists in the DataReader, else returns false

Source

/// <summary>
/// Checks if a column exists in the DataReader
/// </summary>
/// <param name="dr">DataReader</param>
/// <param name="ColumnName">Name of the column to find</param>
/// <returns>Returns true if the column exists in the DataReader, else returns false</returns>
public static Boolean ColumnExists(this IDataReader dr, String ColumnName)
{
    for (Int32 i = 0; i < dr.FieldCount; i++)
        if (dr.GetName(i).Equals(ColumnName, StringComparison.OrdinalIgnoreCase))
            return true;

    return false;
}

Example

Boolean b = datareader.ColumnExists("ColumnNameToCheck");

Author: Jonnidip

Submitted on: 9 dec. 2010

Language: C#

Type: System.Data.IDataReader

Views: 5787