C# - Can you get the column names from a SqlDataReader?

After connecting to the database, can I get the name of all the columns that were returned in my SqlDataReader?

Solution

var reader = cmd.ExecuteReader();

var columns = new List<string>();

for(int i=0;i<reader.FieldCount;i++)
{
   columns.Add(reader.GetName(i));
}

or

var columns = Enumerable.Range(0, reader.FieldCount)
     .Select(reader.GetName).ToList();

Mirror from

StackOverflow - Can you get the column names from a SqlDataReader? - https://stackoverflow.com/questions/681653/can-you-get-the-column-names-from-a-sqldatareader

Previous Post Next Post