How do get the column names for a query?

Posted on 2011-10-05
Last Modified: 2012-08-13
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)
        Reader = Command.ExecuteReader()

        Do While Reader.Read()
Question by:HLRosenberger
    LVL 23

    Accepted Solution

    Check the SqlDataReader.GetSchemaTable Method:

    Something like this:
    ' Displays all columns in 'Reader'
    For Each row In Reader.GetSchemaTable().Rows

    Open in new window

    LVL 2

    Expert Comment

    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

    Open in new window

    LVL 1

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video discusses moving either the default database or any database to a new volume.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now