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
Public NotInheritable Class ConstraintCollection
Inherits InternalDataCollectionBase
Public 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 将确定是否也删除相关行(级联)或其他操作过程。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时,将自动创建 ForeignKeyConstraintUniqueConstraintWhen 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 应用于主键列和外键列,并且将约束添加到子表的 ConstraintCollection中。The 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.

当向 DataTable 对象的 DataColumnCollection添加其 Unique 属性设置为 trueDataColumn 时,将创建一个 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)

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

另请参阅