How to get the column names from a SqlDataReader in C#?

Published on February 15, 2017 by abundantcode

Filed under ADO.NET

Last modified February 15, 2017

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 1822 times

There are times when you might want to connect to a database and then get all the column names that was returned in the SqlDataReader object.

How to get the column names from a SqlDataReader in C#?

Here’s a code snippet demonstrating how to do it.

var slqreaderobj = cmd.ExecuteReader();
var columnnames = Enumerable.Range(0, slqreaderobj.FieldCount).Select(slqreaderobj.GetName).ToList();

Leave a Comment


x

You might be Interested in these Posts

How to Check for Column Name in SqlDataReader object in C# ?

Do you want to check if a column exists in a SqlDataReader instance in C# ?. One of the simplest sol...

How to decode a base64 string in C#?

Here's a sample code snippet demonstrating how you can easily decode a base64 encoded string in C#. ...

How to disable editing of items in a combo box in c#?

When working in Win forms using C# and especially when you use the combo box , you would have notice...