【问题标题】:How to get the table a foreign key refers to如何获取外键引用的表
【发布时间】:2010-10-02 07:34:24
【问题描述】:

我有一个小问题,我还没有找到答案:我如何在 c# 中使用 Microsoft.SqlServer.Smo 获取外键列所指的表?

foreach (Column column in currentTable.Columns) {
        if (column.IsForeignKey) {
                 //GET TABLE FOREIGN KEY REFERS TO
          }
   }

【问题讨论】:

    标签: c# sql-server smo


    【解决方案1】:

    你应该从表本身开始,枚举它的所有外键。示例代码:

    foreach (ForeignKey key in currentTable.ForeignKeys)
    {
        foreach (ForeignKeyColumn column in key.Columns)
        {
            Console.WriteLine("Column: {0} is a foreign key to Table: {1}",column.Name,key.ReferencedTable);
        }
    }
    

    编辑:小改动。在第二个foreach循环中使用foreach(key.Columns中的ForeignKeyColumn列)(我之前有foreach(key.Columns中的列),这是错误的。我的错误。)

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2011-04-23
      • 2016-07-10
      相关资源
      最近更新 更多