How do get the column names for a query?

How do get the column names for a query?  I normally do something like this to get the data.

        Connection = New SqlConnection(the_ConnectionString)
        Command = New SqlCommand(Report_SQL, Connection)
        Connection.Open()
        Reader = Command.ExecuteReader()

        Do While Reader.Read()
        Loop()
LVL 1
HLRosenbergerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wdosanjosCommented:
Check the SqlDataReader.GetSchemaTable Method:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getschematable.aspx

Something like this:
' Displays all columns in 'Reader'
For Each row In Reader.GetSchemaTable().Rows
     Console.WriteLine(row("ColumnName"))
Next

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David11011Commented:
I'm not sure what you are trying to do but here is some code that may help you.
This will get the number of columns in your query table and then loop through the returned values and find all the column names.

 
        Dim CON As New SqlConnection("connectionStringHere")
        Dim myQuery As String = "select * from SQLtable"
        Dim myCommand As SqlCommand = New SqlCommand(myQuery , CON)
        Dim myAdapter As SqlDataAdapter = New SqlDataAdapter(myCommand)
        Dim myDataSet As Data.DataSet = New Data.DataSet
        myAdapter.Fill(myDataSet, "testTable")

        Dim numberOfColumns As Integer = myDataSet.Tables("testTable").Columns.Count
        Dim i As Integer = 0
        Dim ColumnName As String
        Dim myColumnsString As String = Nothing
        Dim myColumnsArray(0, 50) As String

        For i = 0 To numberOfColumns - 1 Step i + 1

            'Get name of the column number that is equal to i
            ColumnName = myDataSet.Tables("testTable").Columns(i).ToString

            'save the column name to a comma delimited string
            myColumnsString = myColumnsString + ", " + myColumnName

            'save the column to an array
            myColumnsArray(0, i) = myColumnName
        Next

Open in new window

0
HLRosenbergerAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.