ConstraintCollection ConstraintCollection ConstraintCollection ConstraintCollection Class

定义

表示 DataTable 的约束集合。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
继承
属性

示例

第一个示例创建一个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两个对象、四个列DataRelation和一个。The second example creates a DataSet, two DataTable objects, four columns, and a DataRelation. 然后,将打印约束的计数,以显示ForeignKeyConstraint在将添加到DataSet对象的DataRelationCollectionDataRelation创建UniqueConstraint和。The 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.

可以包含的UniqueConstraintDataTable对象。 ForeignKeyConstraint ConstraintCollectionThe 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. 确定在更新或删除中的DataTable数据时相关表中将发生的情况。 ForeignKeyConstraintThe ForeignKeyConstraint determines what will occur in related tables when data in the DataTable is either updated or deleted. 例如,如果删除某一行, ForeignKeyConstraint则将确定相关行是否也被删除(级联),或其他一些操作过程。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到时UniqueConstraintForeignKeyConstraint将自动创建和。When you add a DataRelation that creates a relationship between two tables to a DataSet, both a ForeignKeyConstraint and a UniqueConstraint are created automatically. 应用于父DataTable项中的主键列,并且将约束添加到该表的ConstraintCollection中。 UniqueConstraintThe UniqueConstraint is applied to the primary key column in the parent DataTable, and the constraint is added to that table's ConstraintCollection. 应用于主键列和外键列,并将约束添加到子表的ConstraintCollectionForeignKeyConstraintThe ForeignKeyConstraint is applied to the primary key column and the foreign key column, and the constraint is added to the child table's ConstraintCollection.

使用标准集合方法Add,例如、 ClearRemoveConstraintCollectionThe 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.

UniqueConstraint Unique 当的属性true设置DataColumnCollectionDataTable时,将创建一个,并将其属性设置为对象的。 DataColumnA UniqueConstraint is created when a DataColumn with its Unique property set to true is added to a DataTable object's DataColumnCollection.

在将添加到DataSet对象的DataRelationCollection时创建。 ForeignKeyConstraint DataRelationA ForeignKeyConstraint is created when a DataRelation is added to a DataSet object's DataRelationCollection.

属性

Count Count Count Count

获取集合中的元素总数。Gets the total number of elements in a collection.

(Inherited from InternalDataCollectionBase)
IsReadOnly IsReadOnly IsReadOnly IsReadOnly

获取一个值,该值指示 InternalDataCollectionBase 是否为只读。Gets a value that indicates whether the InternalDataCollectionBase is read-only.

(Inherited from InternalDataCollectionBase)
IsSynchronized IsSynchronized IsSynchronized IsSynchronized

获取一个值,该值指示 InternalDataCollectionBase 是否已同步。Gets a value that indicates whether the InternalDataCollectionBase is synchronized.

(Inherited from InternalDataCollectionBase)
Item[Int32] Item[Int32] Item[Int32] Item[Int32]

从集合中获取位于指定索引位置的 ConstraintGets the Constraint from the collection at the specified index.

Item[String] Item[String] Item[String] Item[String]

从具有指定名称的集合中获取 ConstraintGets the Constraint from the collection with the specified name.

List List List List

获取作为列表的集合的项。Gets the items of the collection as a list.

(Inherited from InternalDataCollectionBase)
SyncRoot SyncRoot SyncRoot SyncRoot

获取可用于同步集合的对象。Gets an object that can be used to synchronize the collection.

(Inherited from InternalDataCollectionBase)

方法

Add(Constraint) Add(Constraint) Add(Constraint) Add(Constraint)

将指定的 Constraint 对象添加到集合。Adds the specified Constraint object to the collection.

Add(String, DataColumn, Boolean) Add(String, DataColumn, Boolean) Add(String, DataColumn, Boolean) Add(String, DataColumn, Boolean)

使用指定名称、DataColumn 和指示列是否为主键的值构造一个新 UniqueConstraint,并将其添加到集合中。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) Add(String, DataColumn, DataColumn) Add(String, DataColumn, DataColumn) 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) Add(String, DataColumn[], Boolean) Add(String, DataColumn[], Boolean) Add(String, DataColumn[], Boolean)

使用指定名称、DataColumn 对象的数组和指示列是否为主键的值构造一个新 UniqueConstraint,并将其添加到集合中。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[]) Add(String, DataColumn[], DataColumn[]) Add(String, DataColumn[], DataColumn[]) 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[]) AddRange(Constraint[]) AddRange(Constraint[]) AddRange(Constraint[])

将指定的 ConstraintCollection 数组的元素复制到集合末尾。Copies the elements of the specified ConstraintCollection array to the end of the collection.

CanRemove(Constraint) CanRemove(Constraint) CanRemove(Constraint) CanRemove(Constraint)

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

Clear() Clear() Clear() Clear()

清除所有 Constraint 对象的集合。Clears the collection of any Constraint objects.

Contains(String) Contains(String) Contains(String) Contains(String)

指示集合中是否存在按名称指定的 Constraint 对象。Indicates whether the Constraint object specified by name exists in the collection.

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) 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.

(Inherited from InternalDataCollectionBase)
CopyTo(Constraint[], Int32) CopyTo(Constraint[], Int32) CopyTo(Constraint[], Int32) CopyTo(Constraint[], Int32)

从指定的索引处开始,将集合对象复制到一维 Array 实例中。Copies the collection objects to a one-dimensional Array instance starting at the specified index.

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

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

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

(Inherited from InternalDataCollectionBase)
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)
IndexOf(Constraint) IndexOf(Constraint) IndexOf(Constraint) IndexOf(Constraint)

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

IndexOf(String) IndexOf(String) IndexOf(String) IndexOf(String)

获取按名称指定的 Constraint 的索引。Gets the index of the Constraint specified by name.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(Constraint) Remove(Constraint) Remove(Constraint) Remove(Constraint)

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

Remove(String) Remove(String) Remove(String) Remove(String)

从集合中移除按名称指定的 Constraint 对象。Removes the Constraint object specified by name from the collection.

RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32)

从集合中移除位于指定索引位置的 Constraint 对象。Removes the Constraint object at the specified index from the collection.

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

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

事件

CollectionChanged CollectionChanged CollectionChanged CollectionChanged

每当由于添加或移除 Constraint 对象而更改 ConstraintCollection 时发生。Occurs whenever the ConstraintCollection is changed because of Constraint objects being added or removed.

扩展方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

线程安全性

对于多线程读取操作,此类型是安全的。This type is safe for multithreaded read operations. 必须同步任何写入操作。You must synchronize any write operations.

另请参阅