ConstraintCollection 类

表示 DataTable 的约束的集合。

**命名空间:**System.Data
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public NotInheritable Class ConstraintCollection
    Inherits InternalDataCollectionBase
用法
Dim instance As ConstraintCollection
public sealed class ConstraintCollection : InternalDataCollectionBase
public ref class ConstraintCollection sealed : public InternalDataCollectionBase
public final class ConstraintCollection extends InternalDataCollectionBase
public final class ConstraintCollection extends InternalDataCollectionBase

备注

通过 DataTable.Constraints 属性访问 ConstraintCollection

ConstraintCollection 可以包含 DataTableUniqueConstraintForeignKeyConstraint 对象。UniqueConstraint 对象确保特定列中的数据总是唯一的,以保持数据完整性。ForeignKeyConstraint 确定在更新或删除 DataTable 中的数据时在相关表中将发生什么。例如,如果删除行,ForeignKeyConstraint 将确定是否也删除相关行(级联),或者执行其他某个操作过程。

Note注意

将用于在两个表之间创建关系的 DataRelation 添加到 DataSet 中时,将自动创建 ForeignKeyConstraintUniqueConstraint。将 UniqueConstraint 应用到父级 DataTable 中的主键列,并将约束添加到该表的 ConstraintCollection。将 ForeignKeyConstraint 应用到主键列和外键列,并将约束添加到子表的 ConstraintCollection

ConstraintCollection 使用标准的集合方法,如 AddClearRemove。此外,可以使用 Contains 方法来查找集合中是否存在特定的约束。

在将 DataColumn(其 Unique 属性设置为 true)添加到 DataTable 对象的 DataColumnCollection 中时,将创建 UniqueConstraint

在将 DataRelation 添加到 DataSet 对象的 DataRelationCollection 时,将创建 ForeignKeyConstraint

示例

第一个示例创建一个 DataTable,并将 DataColumn(其 Unique 属性设置为 true)添加到 DataColumnCollection。第二个示例创建一个 DataSet,两个 DataTable 对象,四个列,一个 DataRelation。然后打印出约束的计数,以显示在将 DataRelation 添加到 DataSet 对象的 DataRelationCollection 时,将创建 ForeignKeyConstraintUniqueConstraint

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
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());
        }
    }
}

继承层次结构

System.Object
   System.Data.InternalDataCollectionBase
    System.Data.ConstraintCollection

线程安全

该类型对于多线程读操作是安全的。您必须使任何写操作同步。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

ConstraintCollection 成员
System.Data 命名空间
ForeignKeyConstraint
UniqueConstraint
DataRelation