• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

how list table column names?

Below, I is is the code I am using to connect to our ms sql2000 db. It will fill a list box with the tables there. How do I expand that to print out the column names of one of the tables? Please help with code examples.

I am wanting to use DataColumn, I think, but do not know how. A good start would be to fill a 2nd listbox with the column names from the one in listbox.item(0) for me to learn.

        Dim connection As SqlConnection
        connection = New SqlConnection("Data Source=CUBSQL;Initial Catalog=CompanyA;.......)
        connection.Open()

        'List all the tables in the db
        Dim table As DataTable
        table = connection.GetSchema("Tables")
        For Each row As DataRow In table.Rows 'output list of tables
            ListBox1.Items.Add(row("TABLE_NAME"))
        Next

        connection.Close()

Open in new window

0
Pizpot
Asked:
Pizpot
  • 4
  • 3
1 Solution
 
Ashish PatelCommented:
use this query
select object_name(id), * from syscolumns where object_name(id) = 'tablename'
0
 
Ashish PatelCommented:
Just to list the tablename columns use this

select object_name(id) TableColumns from syscolumns where object_name(id) = 'tablename'
0
 
PizpotAuthor Commented:
Using those queries I get no results. Can you clarify my mistake? Here is my query code, and commented out it one that returns results. What is object_name(id)?

        Dim command As SqlCommand
        Dim reader As SqlDataReader
        'command = New SqlCommand("SELECT ParentPart FROM BomStructure WHERE Component='CM41241'", connection)
        command = New SqlCommand("SELECT object_name(id) TableColumns FROM syscolumns WHERE object_name(id) = 'tablename'", connection)
        reader = command.ExecuteReader() 'executing the command and assigning it to connection
        While reader.Read()
            ListBox2.Items.Add("component" & reader(0).ToString())
        End While
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Ashish PatelCommented:
oh yes you will have to change the 'tablename' with your table name of whome you want the list of columns from. Like if you are looking for list of columns of BomStructure then use this
command = New SqlCommand("SELECT object_name(id) TableColumns FROM syscolumns WHERE object_name(id) = 'BomStructure' ", connection)
0
 
PizpotAuthor Commented:
So close! That returns "BomStructure" 29 times, and I checked and 29 is how many columns BomStructure has. I need more help please.
0
 
PizpotAuthor Commented:
command = New SqlCommand("SELECT * FROM syscolumns WHERE object_name(id) = 'BomStructure' ", connection)

That works. Does it have your blessing?
0
 
PizpotAuthor Commented:
command = New SqlCommand("select column_name from information_schema.columns where table_name='BomStructure'", connection)

is even cooler because you can "select  table_name,column_name,data_type from..." too.
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now