Constraint Constraint Constraint Constraint Class

定義

表示可以在一或多個 DataColumn 物件上強制使用的條件約束。Represents a constraint that can be enforced on one or more DataColumn objects.

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

範例

下列範例DataTable ForeignKeyConstraint會檢查的條件約束集合,並判斷每個條件約束是否為或。UniqueConstraintThe following example examines the collection of constraints for a DataTable and determines whether each constraint is a UniqueConstraint or a ForeignKeyConstraint. 然後會顯示條件約束的屬性。The properties of the constraint are then displayed.

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) 
        {
            PrintForeigKeyConstraintProperties(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 PrintForeigKeyConstraintProperties(
    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資料完整性的規則。A constraint is a rule used to maintain the integrity of the data in the DataTable. 例如,當您刪除一個或多個相關資料表中所使用的值時, ForeignKeyConstraint會判斷相關資料表中的值是否也會一併刪除、設定為 null 值、設定為預設值,或是否不會發生任何動作。For example, when you delete a value that is used in one or more related tables, a ForeignKeyConstraint determines whether the values in the related tables are also deleted, set to null values, set to default values, or whether no action occurs. UniqueConstraint另一方面,只是要確保特定資料表中的所有值都是唯一的。A UniqueConstraint, on the other hand, just makes sure that all values within a particular table are unique. 如需詳細資訊,請參閱DataTable 條件約束For more information, see DataTable Constraints.

不使用Constraint基底的函式。A base Constraint constructor is not used. 主要或唯一索引鍵條件約束是使用UniqueConstraint函式所建立,而 foreign key 條件約束是使用此ForeignKeyConstraint函式來建立。Primary or unique key constraints are created by using the UniqueConstraint constructor, and foreign key constraints are created by using the ForeignKeyConstraint constructor.

建構函式

Constraint() Constraint() Constraint() Constraint()

初始化 Constraint 類別的新執行個體。Initializes a new instance of the Constraint class.

屬性

ConstraintName ConstraintName ConstraintName ConstraintName

ConstraintCollection 中的條件約束名稱。The name of a constraint in the ConstraintCollection.

ExtendedProperties ExtendedProperties ExtendedProperties ExtendedProperties

取得使用者定義條件約束屬性的集合。Gets the collection of user-defined constraint properties.

Table Table Table Table

取得條件約束套用至的 DataTableGets the DataTable to which the constraint applies.

_DataSet _DataSet _DataSet _DataSet

取得這個條件約束所屬的 DataSetGets the DataSet to which this constraint belongs.

方法

CheckStateForProperty() CheckStateForProperty() CheckStateForProperty() CheckStateForProperty()

取得這個條件約束所屬的 DataSetGets the DataSet to which this constraint belongs.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
SetDataSet(DataSet) SetDataSet(DataSet) SetDataSet(DataSet) SetDataSet(DataSet)

設定條件約束的 DataSetSets the constraint's DataSet.

ToString() ToString() ToString() ToString()

取得 ConstraintName (如果有) 做為字串。Gets the ConstraintName, if there is one, as a string.

適用於

執行緒安全性

這種類型對於多執行緒讀取作業而言是安全的。This type is safe for multithreaded read operations. 您必須同步處理任何寫入作業。You must synchronize any write operations.

另請參閱