How to print all columns in a datareader

svon picture svon · Apr 29, 2010 · Viewed 19.3k times · Source

Using c# how do I print all columns in a datareader.

Answer

D'Arcy Rittich picture D'Arcy Rittich · Apr 29, 2010

This method will return an enumerable list of column names when passed a datareader:

static List<string> GetDataReaderColumnNames(IDataReader rdr)
{
    var columnNames = new List<string>();
    for (int i = 0; i < rdr.FieldCount; i++)
        columnNames.Add(rdr.GetName(i));
    return columnNames;
}