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;
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...