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

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();