如何获取表的外键引用

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

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

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

 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中的Column列),这是错误的。我的错误。)