共用方式為


Constraint 類別

定義

表示可以在一或多個 DataColumn 物件上強制使用的條件約束。

public ref class Constraint abstract
[System.ComponentModel.TypeConverter(typeof(System.Data.ConstraintConverter))]
public abstract class Constraint
public abstract class Constraint
[System.ComponentModel.TypeConverter(typeof(System.Data.ConstraintConverter))]
[System.Serializable]
public abstract class Constraint
[<System.ComponentModel.TypeConverter(typeof(System.Data.ConstraintConverter))>]
type Constraint = class
type Constraint = class
[<System.ComponentModel.TypeConverter(typeof(System.Data.ConstraintConverter))>]
[<System.Serializable>]
type Constraint = class
Public MustInherit Class Constraint
繼承
Constraint
衍生
屬性

範例

下列範例會檢查 的條件 DataTable 約束集合,並判斷每個條件約束是否為 UniqueConstraintForeignKeyConstraint。 然後會顯示條件約束的屬性。

private void GetConstraints(DataTable dataTable)
{
    Console.WriteLine();

    // Print the table's name.
    Console.WriteLine("TableName: " + dataTable.TableName);

    // Iterate through the collection and
    // print each name and type value.
    foreach(Constraint constraint in dataTable.Constraints )
    {
        Console.WriteLine("Constraint Name: "
            + constraint.ConstraintName);
        Console.WriteLine("Type: "
            + constraint.GetType().ToString());

        // If the constraint is a UniqueConstraint,
        // print its properties using a function below.
        if(constraint is UniqueConstraint)
        {
            PrintUniqueConstraintProperties(constraint);
        }
        // If the constraint is a ForeignKeyConstraint,
        // print its properties using a function below.
        if(constraint is ForeignKeyConstraint)
        {
            PrintForeignKeyConstraintProperties(constraint);
        }
    }
}

private void PrintUniqueConstraintProperties(
    Constraint constraint)
{
    UniqueConstraint uniqueConstraint;
    uniqueConstraint = (UniqueConstraint) constraint;

    // Get the Columns as an array.
    DataColumn[] columnArray;
    columnArray = uniqueConstraint.Columns;

    // Print each column's name.
    for(int i = 0;i<columnArray.Length ;i++)
    {
        Console.WriteLine("Column Name: "
            + columnArray[i].ColumnName);
    }
}

private void PrintForeignKeyConstraintProperties(
    Constraint constraint)
{
    ForeignKeyConstraint fkConstraint;
    fkConstraint = (ForeignKeyConstraint) constraint;

    // Get the Columns as an array.
    DataColumn[] columnArray;
    columnArray = fkConstraint.Columns;

    // Print each column's name.
    for(int i = 0;i<columnArray.Length ;i++)
    {
        Console.WriteLine("Column Name: "
            + columnArray[i].ColumnName);
    }
    Console.WriteLine();

    // Get the related columns and print each columns name.
    columnArray = fkConstraint.RelatedColumns ;
    for(int i = 0;i<columnArray.Length ;i++)
    {
        Console.WriteLine("Related Column Name: "
            + columnArray[i].ColumnName);
    }
    Console.WriteLine();
}
Private Sub GetConstraints(dataTable As DataTable)
    Console.WriteLine()

    ' Print the table's name.
    Console.WriteLine("TableName: " & dataTable.TableName)

    ' Iterate through the collection and print 
    ' each name and type value.
    Dim constraint As Constraint
    For Each constraint In  dataTable.Constraints
        Console.WriteLine("Constraint Name: " _
            & constraint.ConstraintName)
        Console.WriteLine("Type: " _
            & constraint.GetType().ToString())

        ' If the constraint is a UniqueConstraint, 
        ' print its properties using a function below.
        If TypeOf constraint Is UniqueConstraint Then
            PrintUniqueConstraintProperties(constraint)
        End If

        ' If the constraint is a ForeignKeyConstraint, 
        ' print its properties using a function below.
        If TypeOf constraint Is ForeignKeyConstraint Then
            PrintForeigKeyConstraintProperties(constraint)
        End If
    Next constraint
End Sub

Private Sub PrintUniqueConstraintProperties( _
    constraint As Constraint)

    Dim uniqueConstraint As UniqueConstraint
    uniqueConstraint = CType(constraint, UniqueConstraint)

    ' Get the Columns as an array.
    Dim columnArray() As DataColumn
    columnArray = uniqueConstraint.Columns

    ' Print each column's name.
    Dim i As Integer
    For i = 0 To columnArray.Length - 1
        Console.WriteLine("Column Name: " _
            & columnArray(i).ColumnName)
    Next i
End Sub

Private Sub PrintForeigKeyConstraintProperties( _
    constraint As Constraint)

    Dim fkConstraint As ForeignKeyConstraint
    fkConstraint = CType(constraint, ForeignKeyConstraint)
    
    ' Get the Columns as an array.
    Dim columnArray() As DataColumn
    columnArray = fkConstraint.Columns
    
    ' Print each column's name.
    Dim i As Integer
    For i = 0 To columnArray.Length - 1
        Console.WriteLine("Column Name: " _
            & columnArray(i).ColumnName)
    Next i
    Console.WriteLine()
    
    ' Get the related columns and print each columns name.
    columnArray = fkConstraint.RelatedColumns
    For i = 0 To columnArray.Length - 1
        Console.WriteLine("Related Column Name: " _
            & columnArray(i).ColumnName)
    Next i
    Console.WriteLine()
End Sub

備註

條件約束是用來維護 中 DataTable數據完整性的規則。 例如,當您刪除一或多個相關數據表中使用的值時,會 ForeignKeyConstraint 決定相關數據表中的值是否也會遭到刪除、設定為 Null 值、設定為預設值,還是不會發生任何動作。 另一 UniqueConstraint方面,只要確定特定數據表中的所有值都是唯一的。 如需詳細資訊,請參閱 DataTable 條件約束

不會使用基底 Constraint 建構函式。 主要或唯一索引鍵條件約束是使用建 UniqueConstraint 構函式所建立,而外鍵條件約束是使用 ForeignKeyConstraint 建構函式所建立。

建構函式

Constraint()

初始化 Constraint 類別的新執行個體。

屬性

_DataSet

取得這個條件約束所屬的 DataSet

ConstraintName

ConstraintCollection 中的條件約束名稱。

ExtendedProperties

取得使用者定義條件約束屬性的集合。

Table

取得條件約束套用至的 DataTable

方法

CheckStateForProperty()

取得這個條件約束所屬的 DataSet

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
SetDataSet(DataSet)

設定條件約束的 DataSet

ToString()

取得 ConstraintName (如果有) 做為字串。

適用於

執行緒安全性

此類型適用於多線程讀取作業。 您必須同步處理任何寫入作業。

另請參閱