How to check for the column name in an SqlDataReader Object in C# ?

When working in ADO.NET for data access , there are times when you want to check to see if a column exists in a SqlDataReader object. You can easily find out if the column exists in the SqlDataReader object as shown in the code snippet.

How to check for the column name in an SqlDataReader Object in C# ?

public static bool IsColumnExists(SqlDataReader dataReader, string columnName)
{
    for (int i = 0; i < dataReader.FieldCount; i++)
    {
        if (dataReader.GetName(i).Equals(columnName))
            return true;
    }
    return false;
}

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Reply


x

Interesting BlogPosts to read

How to print title on every page in Microsoft Excel?
Most of the time we might end up having a long workbook with huge amount of data and they have lot o...
How to quickly remove multiple line breaks in Excel using VBA code?
There are times when you might copy few data from different source and you would end up getting unne...
How to change orientation of the worksheets in Microsoft Excel ?
This blog post will show the steps that are needed to change the orientation of the current workshee...