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
Solved

how list table column names?

Posted on 2007-12-06
7
229 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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.
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…
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…

860 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