如何获得一个外键参照该表 [英] How to get the table a foreign key refers to
本文介绍了如何获得一个外键参照该表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个小问题,我没有找到答案尚未:我如何才能在C#和使用Microsoft.SqlServer.Smo一个外键列是指该表?
的foreach(在currentTable.Columns列列){
如果(column.IsForeignKey){
/ / GET表的外键是指
}
}
解决方案
您应该从表本身入手,列举它的所有外键。示例代码:
的foreach(在currentTable.ForeignKeys ForeignKey的键)
{
的foreach(ForeignKeyColumn列key.Columns)
{
Console.WriteLine(列:{0}是一个外键表:{1},column.Name,key.ReferencedTable);
}
}
编辑:小的变化。在第二个foreach循环使用的foreach(以key.Columns ForeignKeyColumn列)(我有它的foreach之前key.Columns(列列),这是错误的。我的错误。)
I have an small question I didn't found an answer yet: how do I get in c# and using Microsoft.SqlServer.Smo the table a foreign key column is referring to?
foreach (Column column in currentTable.Columns) {
if (column.IsForeignKey) {
//GET TABLE FOREIGN KEY REFERS TO
}
}
解决方案
You should start from the table itself, and enumerate all of it's foreign keys. Sample code:
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);
}
}
EDIT: Small change. In second foreach loop use foreach (ForeignKeyColumn column in key.Columns) (I had it foreach (Column column in key.Columns) before, and that's wrong. My mistake.)
这篇关于如何获得一个外键参照该表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文