共用方式為


ForeignKeys 屬性

表示 ForeignKey 物件的集合。每個 ForeignKey 物件代表資料表上定義的外部索引鍵。

命名空間:  Microsoft.SqlServer.Management.Smo
組件:  Microsoft.SqlServer.Smo (在 Microsoft.SqlServer.Smo.dll 中)

語法

'宣告
<SfcObjectAttribute(SfcContainerRelationship.ChildContainer, SfcContainerCardinality.ZeroToAny,  _
    GetType(ForeignKey), SfcObjectFlags.Design)> _
Public ReadOnly Property ForeignKeys As ForeignKeyCollection
    Get
'用途
Dim instance As Table
Dim value As ForeignKeyCollection

value = instance.ForeignKeys
[SfcObjectAttribute(SfcContainerRelationship.ChildContainer, SfcContainerCardinality.ZeroToAny, 
    typeof(ForeignKey), SfcObjectFlags.Design)]
public ForeignKeyCollection ForeignKeys { get; }
[SfcObjectAttribute(SfcContainerRelationship::ChildContainer, SfcContainerCardinality::ZeroToAny, 
    typeof(ForeignKey), SfcObjectFlags::Design)]
public:
property ForeignKeyCollection^ ForeignKeys {
    ForeignKeyCollection^ get ();
}
[<SfcObjectAttribute(SfcContainerRelationship.ChildContainer, SfcContainerCardinality.ZeroToAny, 
    typeof(ForeignKey), SfcObjectFlags.Design)>]
member ForeignKeys : ForeignKeyCollection
function get ForeignKeys () : ForeignKeyCollection

屬性值

型別:Microsoft.SqlServer.Management.Smo. . :: . .ForeignKeyCollection
表示在資料表上定義之所有外部索引鍵的 ForeignKey 物件。

範例

The following code example shows how to display each foreign key in the AdventureWorks2008R2 tables.

C#

Server srv = new Server("(local)");
Database db = srv.Databases["AdventureWorks2008R2"];

Foreach (Table tb in db.Tables) 
{
   foreach (ForeignKey f in tb.ForeignKeys)
   {
      Console.WriteLine("The " + tb.Name + " table contains the " + f.ToString() + " foreign key.");
   }
}

Powershell

$srv = new-Object Microsoft.SqlServer.Management.Smo.Server("(local)")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database
$db = $srv.Databases.Item("AdventureWorks2008R2")

Foreach ($tb in $db.Tables) 
{
   foreach ($f in $tb.ForeignKeys)
   {
      Write-Host "The" $tb.Name "table contains the" $f "foreign key."
   }
}