Check column exists in SqlReader using Linq

Posted on 2011-10-06
Medium Priority
Last Modified: 2012-05-12
The only way to check if a column exists in a SqlDataReader is by looping through the columns


can anyone give me some linq to do this so I can put the check in a single statement?
Question by:purplesoup
  • 3
  • 2
LVL 15

Expert Comment

ID: 36923705

You can try this as well

if (reader.GetSchemaTable().Columns["COLUMNNAME"] != null)



Author Comment

ID: 36923783
This isn't quite right is it - the SchemaTable returned is a table about the resultset, and the columns are ColumnName, ColumnOrdinal, ColumnSize etc, so it will only tell me about the columns in the schema table, not the columns in the resultset.

If for example I want to know if a column called "Sequence" is in my result set,

if (reader.GetSchemaTable().Columns["Sequence"] != null)

won't do it.
LVL 15

Accepted Solution

Navneet earned 2000 total points
ID: 36923841

add this class
    static  class MyName

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

Open in new window

Adding above extension to IDataReader
check your sqlDataReader for Column like :



Author Comment

ID: 36924611
That's pretty clever - I'll wait a little longer if you don't mind just to see if anyone comes up with anything as good...

Author Closing Comment

ID: 37024356
Sorry - I didn't actually intend to wait quite so long..

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question