Avatar of HLRosenberger
HLRosenbergerFlag for United States of America

asked on 

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()
Visual Basic.NETASP.NET

Avatar of undefined
Last Comment
HLRosenberger
ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of David11011
David11011
Flag of United States of America image

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

Avatar of HLRosenberger
HLRosenberger
Flag of United States of America image

ASKER

Thanks!
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo