通过SMO获取ForeignKey的参考

时间:2012-07-16 11:13:52

标签: c# sql-server c#-4.0 smo

我想收集有关特定表格的信息,所以这是我的代码:

StringBuilder sb = new StringBuilder();
sb.AppendLine();
ServerConnection conn = new ServerConnection();
conn.ConnectionString = "";//MyConnection string
Server srv = new Server(conn);
Database db = srv.Databases["MyDataBase"];
Table tb = db.Tables["MyTable"];
foreach(Column c in tb.Columns) {

   sb.AppendLine(c.Name + " -> " + c.DataType.ToString());
   sb.AppendLine(c.ExtendedProperties["MS_Description"].Value.ToString());

  if(c.IsForeignKey){

    //Need to get reference here:
    //Sb.AppendLine("ReferenceColumn")
    //Sb.AppendLine("ReferenceTable")
    //Sb.AppendLine("ReferenceSchema")
  }
}

我找到了this one,但这个链接首先得到Foreignkeys,有没有办法在这里找到引用(用Column类型表示)?在这种情况下你的建议是什么?

1 个答案:

答案 0 :(得分:1)

不确定这是否是您要找的!

foreach (ForeignKey FKey in currentTable.ForeignKeys)
{
    foreach (ForeignKeyColumn FKColumn in FKey.Columns)
    {
        //Hope this is what you are looking for!
        //Check the output of FKColumn.Name
        //Check the output of FKey.ReferencedTable
    }
}