Table.EnumForeignKeys Method
SQL Server 2012
Enumerates a list of primary keys in which the table is referenced as a foreign key.
Namespace: Microsoft.SqlServer.Management.Smo
Assembly: Microsoft.SqlServer.Smo (in Microsoft.SqlServer.Smo.dll)
Return Value
Type: System.Data.DataTableA DataTable system object value that contains a list of information about where this table is referenced as a foreign key. The table describes the different columns of the returned DataTable.
The following code example demonstrates how to enumerate a list of the primary keys that reference the table as a foreign key.
C#
Server srv = new Server("(local)");
Database db = srv.Databases["AdventureWorks2012"];
foreach (Table tb in db.Tables)
{
tb.EnumForeignKeys();
}
Powershell
$srv = new-Object Microsoft.SqlServer.Management.Smo.Server("(local)")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database
$db = $srv.Databases.Item("AdventureWorks2012")
foreach ($tb in $db.Tables)
{
$tb.EnumForeignKeys()
}
