tags:

views:

151

answers:

3

Im using c# windows form application. I have a database with many tables. Each table has several columns. I need to populate the combo box with the column names for a selected table.

+1  A: 

This should give you the list of tables

SELECT name 
FROM dbo.sysobjects 
WHERE xtype = 'U' 

And this should give you column information per table

select * 
from INFORMATION_SCHEMA.COLUMNS 
where TABLE_NAME = 'yourTable'
astander
"sysobjects" is deprecated as of SQL Server 2005 - please start using "sys.objects" and the other catalog views in the "sys" schema
marc_s
A: 

Hi,

Here is how you can query the column names from sql server. Other databases are similar to this. http://blog.sqlauthority.com/2008/08/05/sql-server-2005-get-field-name-and-type-of-database-table/

As far as getting the items into a combobox you can find volumes of information on this on msdn at this link http://msdn.microsoft.com/en-us/library/aa983551(VS.71).aspx

Enjoy!

Doug
+1  A: 

The proper way of doing this on SQL Server 2005 and up (you didn't specify quite clearly what version you're using) would be to inspect the system catalog view, which live in the sys schema:

SELECT 
    name
FROM    
    sys.columns
WHERE
    object_id = object_id('YourTableNameHere')
ORDER BY
    name    

The sys.columns catalog view gives you quite a bit of information about the columns for a table - the table is identified by the object_id column, which is the internal ID for that table in question.

marc_s