Return to Snippet

Revision: 45935
at May 11, 2011 08:30 by emregulcan


Initial Code
public static bool IsColumnExists(this IDataReader dataReader, string columnName)
        {
            bool retVal = false;

            try
            {
                dataReader.GetSchemaTable().DefaultView.RowFilter = string.Format("ColumnName= '{0}'", columnName);
                if (dataReader.GetSchemaTable().DefaultView.Count > 0)
                {
                    retVal = true;
                }
            }

            catch (Exception ex)
            {
                
                throw;
            }

            return retVal;
        }

Initial URL

                                

Initial Description
This snippet is "extension method" for IDataReader. 
You can use this snippet below ;
I assume you have datareader like that
SqlDataReader dataReader = command.ExecuteReader();

if (dataReader.IsColumnExists("columnName"))
{
   //do something
}


and also you should validation null value control if you work critical datas

if (dataReader.IsColumnExists("columName"))
{
    if (dataReader["columName"] != DBNull.Value)
       {
             //do something
        }
}

Initial Title
Check SqlDataReader Column Is Exists  (extension method)

Initial Tags
sql, c

Initial Language
C#