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

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...