Solved

how list table column names?

Posted on 2007-12-06
7
241 Views
Last Modified: 2013-12-25
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
Comment
Question by:Pizpot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20421105
use this query
select object_name(id), * from syscolumns where object_name(id) = 'tablename'
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20421116
Just to list the tablename columns use this

select object_name(id) TableColumns from syscolumns where object_name(id) = 'tablename'
0
 

Author Comment

by:Pizpot
ID: 20421279
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 23

Accepted Solution

by:
Ashish Patel earned 500 total points
ID: 20421319
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
 

Author Comment

by:Pizpot
ID: 20421413
So close! That returns "BomStructure" 29 times, and I checked and 29 is how many columns BomStructure has. I need more help please.
0
 

Author Comment

by:Pizpot
ID: 20421601
command = New SqlCommand("SELECT * FROM syscolumns WHERE object_name(id) = 'BomStructure' ", connection)

That works. Does it have your blessing?
0
 

Author Comment

by:Pizpot
ID: 20421738
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

734 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