ConstraintCollection 類別

定義

表示為 DataTable 的條件約束 (Constraint) 集合。Represents a collection of constraints for a DataTable.

public ref class ConstraintCollection sealed : System::Data::InternalDataCollectionBase
[System.Serializable]
public sealed class ConstraintCollection : System.Data.InternalDataCollectionBase
type ConstraintCollection = class
    inherit InternalDataCollectionBase
Public NotInheritable Class ConstraintCollection
Inherits InternalDataCollectionBase
繼承
ConstraintCollection
屬性

範例

第一個範例會建立 DataTable,並將 DataColumn (其 Unique 屬性設定為 true)新增至 DataColumnCollectionThe first example creates a DataTable, and adds a DataColumn (with its Unique property set to true) to the DataColumnCollection. 第二個範例會建立一個 DataSet、兩個 DataTable 物件、四個數據行和一個 DataRelationThe second example creates a DataSet, two DataTable objects, four columns, and a DataRelation. 然後會列印條件約束的計數,以顯示當 DataRelation 新增至 DataSet 物件的 DataRelationCollection時,會建立 ForeignKeyConstraintUniqueConstraintThe count of constraints is then printed to show that a ForeignKeyConstraint and a UniqueConstraint are created when a DataRelation is added to the DataSet object's DataRelationCollection.

   private void MakeTableWithUniqueConstraint()
   {
       DataTable table = new DataTable("table");
       DataColumn column = new DataColumn("UniqueColumn");
       column.Unique=true;
       table.Columns.Add(column);

       // Print count, name, and type.
       Console.WriteLine("Constraints.Count " 
           + table.Constraints.Count);
       Console.WriteLine(table.Constraints[0].ConstraintName);
       Console.WriteLine(table.Constraints[0].GetType() );

       // Add a second unique column.
       column = new DataColumn("UniqueColumn2");
       column.Unique=true;
       table.Columns.Add(column);

       // Print info again.
       Console.WriteLine("Constraints.Count " 
           + table.Constraints.Count);
       Console.WriteLine(table.Constraints[1].ConstraintName);
       Console.WriteLine(table.Constraints[1].GetType() );
   }
    
   private void MakeTableWithForeignConstraint()
   {
       // Create a DataSet.
       DataSet dataSet = new DataSet("dataSet");

       // Make two tables.
       DataTable customersTable= new DataTable("Customers");
       DataTable ordersTable = new DataTable("Orders");

       // Create four columns, two for each table.
       DataColumn name = new DataColumn("Name");
       DataColumn id = new DataColumn("ID");
       DataColumn orderId = new DataColumn("OrderID");
       DataColumn cDate = new DataColumn("OrderDate");

       // Add columns to tables.
       customersTable.Columns.Add(name);
       customersTable.Columns.Add(id);
       ordersTable.Columns.Add(orderId);
       ordersTable.Columns.Add(cDate);

       // Add tables to the DataSet.
       dataSet.Tables.Add(customersTable);
       dataSet.Tables.Add(ordersTable); 

       // Create a DataRelation for two of the columns.
       DataRelation myRelation = new 
           DataRelation("CustomersOrders",id,orderId,true);
       dataSet.Relations.Add(myRelation);

       // Print TableName, Constraints.Count, 
       // ConstraintName and Type.
       foreach(DataTable t in dataSet.Tables)
       {
           Console.WriteLine(t.TableName);
           Console.WriteLine("Constraints.Count " 
               + t.Constraints.Count);
           Console.WriteLine("ParentRelations.Count " 
               + t.ParentRelations.Count);
           Console.WriteLine("ChildRelations.Count " 
               + t.ChildRelations.Count);
           foreach(Constraint cstrnt in t.Constraints)
           {
               Console.WriteLine(cstrnt.ConstraintName);
               Console.WriteLine(cstrnt.GetType());
           }
       }
   }
Private Sub MakeTableWithUniqueConstraint()

    Dim table As New DataTable("table")
    Dim column As New DataColumn("UniqueColumn")
    column.Unique = True
    table.Columns.Add(column)

    ' Print count, name, and type.
    Console.WriteLine("Constraints.Count " _
       + table.Constraints.Count.ToString())
    Console.WriteLine(table.Constraints(0).ConstraintName)
    Console.WriteLine( _
        table.Constraints(0).GetType().ToString())

    ' Add a second unique column.
    column = New DataColumn("UniqueColumn2")
    column.Unique = True
    table.Columns.Add(column)

    ' Print info again.
    Console.WriteLine("Constraints.Count " _
       + table.Constraints.Count.ToString())
    Console.WriteLine(table.Constraints(1).ConstraintName)
    Console.WriteLine( _
        table.Constraints(1).GetType().ToString())
End Sub

Private Sub MakeTableWithForeignConstraint()

    ' Create a DataSet.
    Dim dataSet As New DataSet("dataSet")

    ' Make two tables.
    Dim customersTable As New DataTable("Customers")
    Dim ordersTable As New DataTable("Orders")

    ' Create four columns, two for each table.
    Dim name As New DataColumn("Name")
    Dim id As New DataColumn("ID")
    Dim orderId As New DataColumn("OrderID")
    Dim orderDate As New DataColumn("OrderDate")
    
    ' Add columns to tables.
    customersTable.Columns.Add(name)
    customersTable.Columns.Add(id)
    ordersTable.Columns.Add(orderId)
    ordersTable.Columns.Add(orderDate)
    
    ' Add tables to the DataSet.
    dataSet.Tables.Add(customersTable)
    dataSet.Tables.Add(ordersTable)

    ' Create a DataRelation for two of the columns.
    Dim myRelation As New DataRelation _
       ("CustomersOrders", id, orderId, True)
    dataSet.Relations.Add(myRelation)

    ' Print TableName, Constraints.Count, 
    ' ConstraintName and Type.
    Dim t As DataTable
    For Each t In  dataSet.Tables
        Console.WriteLine(t.TableName)
        Console.WriteLine("Constraints.Count " _
           + t.Constraints.Count.ToString())
        Console.WriteLine("ParentRelations.Count " _
           + t.ParentRelations.Count.ToString())
        Console.WriteLine("ChildRelations.Count " _
           + t.ChildRelations.Count.ToString())
        Dim cstrnt As Constraint
        For Each cstrnt In  t.Constraints
            Console.WriteLine(cstrnt.ConstraintName)
            Console.WriteLine(cstrnt.GetType())
        Next cstrnt
    Next t
End Sub

備註

ConstraintCollection 是透過 DataTable.Constraints 屬性來存取。The ConstraintCollection is accessed through the DataTable.Constraints property.

ConstraintCollection 可以同時包含 DataTableUniqueConstraintForeignKeyConstraint 物件。The ConstraintCollection can contain both UniqueConstraint and ForeignKeyConstraint objects for the DataTable. UniqueConstraint 物件可確保特定資料行中的資料一律是唯一的,以保留資料完整性。A UniqueConstraint object makes sure that data in a specific column is always unique to preserve the data integrity. ForeignKeyConstraint 決定當 DataTable 中的資料已更新或刪除時,相關資料表中會發生什麼事。The ForeignKeyConstraint determines what will occur in related tables when data in the DataTable is either updated or deleted. 例如,如果刪除了資料列,ForeignKeyConstraint 將會決定相關資料列是否也會一併刪除(cascade),或其他動作。For example, if a row is deleted, the ForeignKeyConstraint will determine whether the related rows are also deleted (a cascade), or some other course of action.

注意

當您將建立兩個數據表之間關聯性的 DataRelation 新增至 DataSet時,ForeignKeyConstraintUniqueConstraint 都會自動建立。When you add a DataRelation that creates a relationship between two tables to a DataSet, both a ForeignKeyConstraint and a UniqueConstraint are created automatically. UniqueConstraint 會套用至父 DataTable中的主鍵資料行,並將條件約束加入該資料表的 ConstraintCollection中。The UniqueConstraint is applied to the primary key column in the parent DataTable, and the constraint is added to that table's ConstraintCollection. ForeignKeyConstraint 會套用至主鍵資料行和外鍵資料行,並將條件約束加入至子資料工作表的 ConstraintCollectionThe ForeignKeyConstraint is applied to the primary key column and the foreign key column, and the constraint is added to the child table's ConstraintCollection.

ConstraintCollection 使用標準的集合方法,例如 AddClearRemoveThe ConstraintCollection uses standard collection methods such as Add, Clear, and Remove. 此外,Contains 方法也可以用來尋找集合中特定條件約束是否存在。In addition, the Contains method can be used to look for the existence of a particular constraint in the collection.

Unique 屬性設為 trueDataColumn 加入至 DataTable 物件的 DataColumnCollection時,就會建立 UniqueConstraintA UniqueConstraint is created when a DataColumn with its Unique property set to true is added to a DataTable object's DataColumnCollection.

DataRelation 新增至 DataSet 物件的 DataRelationCollection時,就會建立 ForeignKeyConstraintA ForeignKeyConstraint is created when a DataRelation is added to a DataSet object's DataRelationCollection.

屬性

Count

取得集合中的項目總數。Gets the total number of elements in a collection.

(繼承來源 InternalDataCollectionBase)
IsReadOnly

取得值,這個值表示 InternalDataCollectionBase 是否為唯讀。Gets a value that indicates whether the InternalDataCollectionBase is read-only.

(繼承來源 InternalDataCollectionBase)
IsSynchronized

取得值,表示 InternalDataCollectionBase 是否同步處理。Gets a value that indicates whether the InternalDataCollectionBase is synchronized.

(繼承來源 InternalDataCollectionBase)
Item[Int32]

從集合的指定索引處取得 ConstraintGets the Constraint from the collection at the specified index.

Item[String]

以指定名稱從集合取得 ConstraintGets the Constraint from the collection with the specified name.

List

取得集合的項目做為清單。Gets the items of the collection as a list.

(繼承來源 InternalDataCollectionBase)
SyncRoot

取得可以用來同步集合的物件。Gets an object that can be used to synchronize the collection.

(繼承來源 InternalDataCollectionBase)

方法

Add(Constraint)

將指定的 Constraint 物件加入至集合。Adds the specified Constraint object to the collection.

Add(String, DataColumn, Boolean)

使用指定的名稱、UniqueConstraint 和表示資料行是否為主索引鍵的值,來建構新的 DataColumn,並將它加入至集合中。Constructs a new UniqueConstraint with the specified name, DataColumn, and value that indicates whether the column is a primary key, and adds it to the collection.

Add(String, DataColumn, DataColumn)

使用指定的名稱、父資料行和子資料行建構新的 ForeignKeyConstraint,並將條件約束加入至集合。Constructs a new ForeignKeyConstraint with the specified name, parent column, and child column, and adds the constraint to the collection.

Add(String, DataColumn[], Boolean)

使用指定的名稱、UniqueConstraint 物件的陣列,以及表示資料行是否為主索引鍵的值,來建構新的 DataColumn,並將它加入至集合中。Constructs a new UniqueConstraint with the specified name, array of DataColumn objects, and value that indicates whether the column is a primary key, and adds it to the collection.

Add(String, DataColumn[], DataColumn[])

使用父資料行和子資料行的指定陣列,建構新的 ForeignKeyConstraint,並將條件約束加入至集合。Constructs a new ForeignKeyConstraint, with the specified arrays of parent columns and child columns, and adds the constraint to the collection.

AddRange(Constraint[])

將指定的 ConstraintCollection 陣列元素複製到集合的末端。Copies the elements of the specified ConstraintCollection array to the end of the collection.

CanRemove(Constraint)

指示是否能移除 ConstraintIndicates whether a Constraint can be removed.

Clear()

清除任何 Constraint 物件的集合。Clears the collection of any Constraint objects.

Contains(String)

指示依名稱所指定的 Constraint 物件是否存在於集合中。Indicates whether the Constraint object specified by name exists in the collection.

CopyTo(Array, Int32)

複製目前 InternalDataCollectionBase 的所有項目至一維 Array,開始於指定的 InternalDataCollectionBase 索引。Copies all the elements of the current InternalDataCollectionBase to a one-dimensional Array, starting at the specified InternalDataCollectionBase index.

(繼承來源 InternalDataCollectionBase)
CopyTo(Constraint[], Int32)

將集合物件複製到指定索引開頭的一維 Array 執行個體。Copies the collection objects to a one-dimensional Array instance starting at the specified index.

Equals(Object)

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

(繼承來源 Object)
GetEnumerator()

取得集合的 IEnumeratorGets an IEnumerator for the collection.

(繼承來源 InternalDataCollectionBase)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

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

(繼承來源 Object)
IndexOf(Constraint)

取得指定 Constraint 的索引。Gets the index of the specified Constraint.

IndexOf(String)

取得根據名稱所指定的 Constraint 之索引。Gets the index of the Constraint specified by name.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
Remove(Constraint)

從集合移除指定的 ConstraintRemoves the specified Constraint from the collection.

Remove(String)

從集合中移除名稱所指定的 Constraint 物件。Removes the Constraint object specified by name from the collection.

RemoveAt(Int32)

從集合移除指定之索引處的 Constraint 物件。Removes the Constraint object at the specified index from the collection.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

事件

CollectionChanged

由於正在加入或移除 ConstraintCollection 物件而導致 Constraint 變更時,就會發生此情況。Occurs whenever the ConstraintCollection is changed because of Constraint objects being added or removed.

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

執行緒安全性

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

另請參閱