DataSet.EnforceConstraints 属性

定义

获取或设置一个值,该值指示在尝试执行任何更新操作时是否遵循约束规则。Gets or sets a value indicating whether constraint rules are followed when attempting any update operation.

public:
 property bool EnforceConstraints { bool get(); void set(bool value); };
public bool EnforceConstraints { get; set; }
[System.Data.DataSysDescription("DataSetEnforceConstraintsDescr")]
public bool EnforceConstraints { get; set; }
member this.EnforceConstraints : bool with get, set
[<System.Data.DataSysDescription("DataSetEnforceConstraintsDescr")>]
member this.EnforceConstraints : bool with get, set
Public Property EnforceConstraints As Boolean

属性值

Boolean

true 如果强制执行规则,则为;否则为 falsetrue if rules are enforced; otherwise, false. 默认值为 trueThe default is true.

属性

例外

无法实施一个或多个约束。One or more constraints cannot be enforced.

示例

下面的示例创建一个 DataSet 具有一个表、一个列、五行和一个表的 UniqueConstraintThe following example creates a DataSet with one table, one column, five rows, and one UniqueConstraint. EnforceConstraints属性设置为 false ,每行的值都设置为相同的值。The EnforceConstraints property is set to false and the values of each row are set to the same value. 如果将 EnforceConstraints 属性重置为 trueConstraintException 则会生成。When the EnforceConstraints property is reset to true, a ConstraintException is generated.

private void DemonstrateEnforceConstraints()
{
    // Create a DataSet with one table, one column and
    // a UniqueConstraint.
    DataSet dataSet= new DataSet("dataSet");
    DataTable table = new DataTable("table");
    DataColumn column = new DataColumn("col1");

    // A UniqueConstraint is added when the Unique
    // property is true.
    column.Unique=true;
    table.Columns.Add(column);
    dataSet.Tables.Add(table);
    Console.WriteLine("constraints.count: " +
        table.Constraints.Count);

    // add five rows.
    DataRow row ;
    for(int i=0;i<5;i++)
    {
        row = table.NewRow();
        row["col1"] = i;
        table.Rows.Add(row);
    }
    table.AcceptChanges();

    dataSet.EnforceConstraints=false;
    // Change the values of all rows to 1.
    foreach(DataRow thisRow in table.Rows)
    {
        thisRow["col1"]=1;
        //Console.WriteLine("\table" + thisRow[0]);
    }
    try
    {
        dataSet.EnforceConstraints=true;
    }
    catch(System.Data.ConstraintException e)
    {
        // Process exception and return.
        Console.WriteLine("Exception of type {0} occurred.",
            e.GetType());
    }
}
Private Sub DemonstrateEnforceConstraints()
    ' Create a DataSet with one table, one column and 
    ' a UniqueConstraint.
    Dim dataSet As New DataSet("dataSet")
    Dim table As New DataTable("table")
    Dim column As New DataColumn("col1")
    column.Unique = True
    table.Columns.Add(column)
    dataSet.Tables.Add(table)
    Console.WriteLine("constraints.count: " _
        & table.Constraints.Count)

    ' add five rows.
    Dim row As DataRow
    Dim i As Integer
    For i = 0 To 4
       row = table.NewRow()
       row("col1") = i
       table.Rows.Add(row)
    Next
    table.AcceptChanges()
     
    dataSet.EnforceConstraints = False
    ' Change the values of all rows to 1.
    Dim thisRow As DataRow
    For Each thisRow In table.rows
       thisRow("col1") = 1
    Next
 
    Try
        dataSet.EnforceConstraints = True
    Catch e As System.Data.ConstraintException
        ' Process exception and return.
        Console.WriteLine("Exception of type {0} occurred.", _
            e.GetType().ToString())
    End Try
End Sub

注解

DataTable 级别 (属性) 设置约束 ConstraintsConstraints are set at the DataTable level (Constraints property). 有关创建约束的详细信息,请参阅 DataTable 约束For more information about creating constraints, see DataTable Constraints.

适用于

另请参阅