Check column exists in SqlReader using Linq

Posted on 2011-10-06
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
    LVL 15

    Expert Comment


    You can try this as well

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



    Author Comment

    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


    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

    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

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now