使用Microsoft.SqlServer.Management.Smo枚举表列

时间:2013-08-07 19:33:55

标签: sql-server smo

亲爱的数据空间主,如果有人听我的请求,

我的代码看起来像这样,

string ConnectionString = "Data Source=AServerName;Initial Catalog=ADBName;Integrated Security=SSPI;";
string TableName = "ATableName";
string SchemaName = "dbo";

Server server = new Server(new ServerConnection(new SqlConnection(ConnectionString)));
SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(ConnectionString);
Database db = new Database(server, connectionStringBuilder.InitialCatalog);
db.Refresh();

Table _table = new Table(db, TableName, SchemaName);
_table.Refresh();

它通过了身份验证/授权,但_table.columns.Count为零,但db.Tables.Count具有良好的价值,所以我想知道如何填充_table

请帮助,谢谢。

如果遗漏任何细节或有任何疑问,请随时告诉我。

1 个答案:

答案 0 :(得分:2)

获得Database后,只需检查.Tables财产:

Database db = new Database(server, connectionStringBuilder.InitialCatalog);
db.Refresh();

foreach(var table in db.Tables)
{
   // do something with each table here...
   string tableName = table.Name;

   foreach(var column in table.Columns)
   {
       // do something with each column for that table
       string columnName = column.Name;
   }
}