Solved

how list table column names?

Posted on 2007-12-06
7
221 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb. Net application freezes 9 45
SqlServer no dupes 25 34
Sql server insert 13 27
Sending receiving text messages in vb.net 15 32
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

808 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