Get a list of columns

I'm trying to get a list of columns from a table. Here is my code:

publicList<string> GetColumns(string serverName,string dataBaseName,string tableName)

{

List<string> columns =newList<string>();

Server server =newServer(serverName);

server.ConnectionContext.LoginSecure =true;

server.ConnectionContext.Connect();

Database dataBase =newDatabase(server, dataBaseName);

Table table =newTable(dataBase, tableName);

foreach (Column columnin table.Columns)

{

columns.Add(column.Name);

}

columns.Sort();

return columns;

}

The problem is that table.Columns.Count is 0. How do I get a list of columns in a table?

[2949 byte] By [DanJurden] at [2008-2-20]
# 1

I got this working. Instead of getting a reference to each object individually:

Database dataBase = new Database(server, dataBaseName);

Table table = new Table(dataBase, tableName);

Then looping through the Columns collection of the table, I combined it all in one:

foreach (Column column in server.Database[dataBaseName].Tables[tableName].Columns)

This now gets me a list of all the columns in the table.

DanJurden at 2007-10-7 > top of Msdn Tech,SQL Server,SQL Server SMO/DMO...

SQL Server

Site Classified