ConstraintCollection.AddRange 方法

将指定的 ConstraintCollection 数组的元素复制到集合末尾。

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

语法

声明
Public Sub AddRange ( _
    constraints As Constraint() _
)
用法
Dim instance As ConstraintCollection
Dim constraints As Constraint()

instance.AddRange(constraints)
public void AddRange (
    Constraint[] constraints
)
public:
void AddRange (
    array<Constraint^>^ constraints
)
public void AddRange (
    Constraint[] constraints
)
public function AddRange (
    constraints : Constraint[]
)

参数

备注

如果已经调用 BeginInit,则在调用 EndInit 之前,AddRange 将不会向集合中添加任何对象。当调用 EndInit 时,将使用在最近一次对 AddRange 的调用中指定的项填充集合。如果在 BeginInit / EndInit 序列中多次调用了 AddRange,则只添加在最近一次对 AddRange 的调用中指定的那些项。

示例

下面的示例创建主键约束和外键约束,并将它们添加到 ConstraintCollection 中。

Public Shared Sub ConstraintAddRange(dataSet As DataSet)
    Try
        ' Reference the tables from the DataSet.
        Dim customersTable As DataTable = dataSet.Tables("Customers")
        Dim ordersTable As DataTable = dataSet.Tables("Orders")

        ' Create unique and foreign key constraints.
        Dim uniqueConstraint As UniqueConstraint = New _
            UniqueConstraint(customersTable.Columns("CustomerID"))
        Dim fkConstraint As ForeignKeyConstraint = New _
            ForeignKeyConstraint("CustOrdersConstraint", _
            customersTable.Columns("CustomerID"), _
            ordersTable.Columns("CustomerID"))

        ' Add the constraints.
        customersTable.Constraints.AddRange(New Constraint() _
            {uniqueConstraint, fkConstraint})

    Catch ex As Exception
        ' Process exception and return.
        Console.WriteLine("Exception of type {0} occurred.", _
        ex.GetType().ToString())
    End Try
End Sub
public static void ConstraintAddRange(DataSet dataSet)
{
    try
    {
        // Reference the tables from the DataSet.
        DataTable customersTable = dataSet.Tables["Customers"];
        DataTable ordersTable = dataSet.Tables["Orders"];

        // Create unique and foreign key constraints.
        UniqueConstraint uniqueConstraint = new 
            UniqueConstraint(customersTable.Columns["CustomerID"]);
        ForeignKeyConstraint fkConstraint = new 
            ForeignKeyConstraint("CustOrdersConstraint",
            customersTable.Columns["CustomerID"],
            ordersTable.Columns["CustomerID"]);

        // Add the constraints.
        customersTable.Constraints.AddRange(new Constraint[] 
            {uniqueConstraint, fkConstraint});
    }
    catch(Exception ex)
    {
        // Process exception and return.
        Console.WriteLine("Exception of type {0} occurred.", 
            ex.GetType());
    }
}

平台

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 类
ConstraintCollection 成员
System.Data 命名空间