ConstraintCollection 类

定义

表示 DataTable 的约束集合。Represents a collection of constraints for a DataTable.

public ref class ConstraintCollection sealed : System::Data::InternalDataCollectionBase
public ref class ConstraintCollection : System::Data::InternalDataCollectionBase
public sealed class ConstraintCollection : System.Data.InternalDataCollectionBase
[System.Serializable]
public class ConstraintCollection : System.Data.InternalDataCollectionBase
type ConstraintCollection = class
    inherit InternalDataCollectionBase
[<System.Serializable>]
type ConstraintCollection = class
    inherit InternalDataCollectionBase
Public NotInheritable Class ConstraintCollection
Inherits InternalDataCollectionBase
Public Class ConstraintCollection
Inherits InternalDataCollectionBase
继承
ConstraintCollection
属性

示例

第一个示例创建一个 DataTable ,并添加 DataColumnUnique 属性设置为 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. 然后,将打印约束的计数,以显示在将 ForeignKeyConstraint UniqueConstraint DataRelation 添加到对象的时创建和 DataSet DataRelationCollectionThe 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可以包含 UniqueConstraint ForeignKeyConstraint 的和对象 DataTableThe 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确定在更新或删除中的数据时相关表中将发生的情况 DataTableThe 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 ,将 ForeignKeyConstraint UniqueConstraint 自动创建和。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 ,并且将约束添加到该表的中 ConstraintCollectionThe 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使用标准集合方法 Add ,例如、 ClearRemoveThe 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 DataColumnUnique 属性设置为 true DataTable 对象的 DataColumnCollectionA UniqueConstraint is created when a DataColumn with its Unique property set to true is added to a DataTable object's DataColumnCollection.

ForeignKeyConstraint DataRelation 将添加到 DataSet 对象的时创建 DataRelationCollectionA 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.

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)

使用指定名称、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)

使用指定名称、父列和子列来构造新 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)

使用指定名称、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[])

使用指定的父列和子列数组,构造新的 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)
OnCollectionChanged(CollectionChangeEventArgs)

引发 CollectionChanged 事件。Raises the CollectionChanged event.

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

每当由于添加或移除 Constraint 对象而更改 ConstraintCollection 时发生。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.

另请参阅