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 3045 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...

3 New C# 8 Features We Are Excited About

C# is rapidly approaching its third decade of life. Originally released in 2000, the language has gr...

Quick Tip —CSS Containment Module by @gilfink

One of the new hidden gems in the new CSS modules, which is supposed to drop in new browser versions...