DataSet.EnforceConstraints 属性

获取或设置一个值,该值指示在尝试执行任何更新操作时是否遵循约束规则。

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

语法

声明
Public Property EnforceConstraints As Boolean
用法
Dim instance As DataSet
Dim value As Boolean

value = instance.EnforceConstraints

instance.EnforceConstraints = value
public bool EnforceConstraints { get; set; }
public:
property bool EnforceConstraints {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_EnforceConstraints ()

/** @property */
public void set_EnforceConstraints (boolean value)
public function get EnforceConstraints () : boolean

public function set EnforceConstraints (value : boolean)

属性值

如果实施规则,则为 true;否则为 false。默认为 true

异常

异常类型 条件

ConstraintException

无法实施一个或多个约束。

备注

约束在 DataTable 级别(Constraints 属性)设置。有关创建约束的更多信息,请参见 将约束添加到表

示例

下面的示例创建一个 DataSet,该数据集包含一个表、一个列、五个行和一个 UniqueConstraintEnforceConstraints 属性被设置为 false,每一行的值都设置为同一个值。如果 EnforceConstraints 属性被重置为 true,则生成 ConstraintException

Private Sub DemonstrateEnforceConstraints()
    ' Create a DataSet with one table, one column and 
    ' a UniqueConstraint.
    Dim dataSet As DataSet = New DataSet("dataSet")
    Dim table As DataTable = New DataTable("table")
    Dim column As DataColumn = 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
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());
    }
}

平台

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

请参见

参考

DataSet 类
DataSet 成员
System.Data 命名空间

其他资源

在 ADO.NET 中使用 DataSet