Solved

how list table column names?

Posted on 2007-12-06
7
204 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

757 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

20 Experts available now in Live!

Get 1:1 Help Now