Export (0) Print
Expand All

DataSet.EnforceConstraints Property

Gets or sets a value indicating whether constraint rules are followed when attempting any update operation.

[Visual Basic]
Public Property EnforceConstraints As Boolean
[C#]
public bool EnforceConstraints {get; set;}
[C++]
public: __property bool get_EnforceConstraints();
public: __property void set_EnforceConstraints(bool);
[JScript]
public function get EnforceConstraints() : Boolean;
public function set EnforceConstraints(Boolean);

Property Value

true, if rules are enforced; otherwise, false. The default is true.

Exceptions

Exception Type Condition
ConstraintException One or more constraints cannot be enforced.

Remarks

Constraints are set at the DataTable level (Constraints property). For more information about creating constraints, see Adding Constraints to a Table.

Example

[Visual Basic, C#, C++] The following example creates a DataSet with one table, one column, five rows, and one UniqueConstraint. The EnforceConstraints property is set to false and the values of each row are set to the same value. When the EnforceConstraints property is reset to true, a ConstraintException is generated.

[Visual Basic] 
Private Sub DemonstrateEnforceConstraints()
   ' Create a DataSet with one table, one column and a UniqueConstraint.
   Dim ds As DataSet = New DataSet("myDataSet")
   Dim t As DataTable = New DataTable("myTable")
   Dim c As DataColumn = New DataColumn("col1")
   c.Unique = True
   t.Columns.Add(c)
   ds.Tables.Add(t)
   Console.WriteLine("constraints.count: " & t.Constraints.Count)
   ' add five rows.
   Dim r As DataRow
   Dim i As Integer
   For i = 0 To 4

      r = t.NewRow()
      r("col1") = i
      t.Rows.Add(r)
   Next
   t.AcceptChanges()
    
   ds.EnforceConstraints = False
   ' Change the values of all rows to 1.
   Dim thisRow As DataRow
   For Each thisRow In t.rows
      thisRow("col1") = 1
   Next

   Try
       ds.EnforceConstraints = True
   Catch e As System.Data.ConstraintException
   ' Process exception and return.
       Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
       log.Source = "My Application"
       log.WriteEntry(e.ToString())
       Console.WriteLine("Exception of type {0} occurred.", e.GetType().ToString())
   End Try
End Sub

[C#] 
private void DemonstrateEnforceConstraints(){
   // Create a DataSet with one table, one column and a UniqueConstraint.
   DataSet ds= new DataSet("myDataSet");
   DataTable t = new DataTable("myTable");
   DataColumn c = new DataColumn("col1");
   // A UniqueConstraint is added when the Unique property is true.
   c.Unique=true;
   t.Columns.Add(c);
   ds.Tables.Add(t);
   Console.WriteLine("constraints.count: " + t.Constraints.Count);
   // add five rows.
   DataRow r ;
   for(int i=0;i<5;i++){
      r = t.NewRow();
      r["col1"] = i;
      t.Rows.Add(r);
   }
   t.AcceptChanges();
   
   ds.EnforceConstraints=false;
   // Change the values of all rows to 1.
   foreach(DataRow thisRow in t.Rows){
      thisRow["col1"]=1;
      //Console.WriteLine("\t" + thisRow[0]);
   }
   try{
      ds.EnforceConstraints=true;
   }
   catch(System.Data.ConstraintException e){
   // Process exception and return.
       System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
       log.Source = "My Application";
       log.WriteEntry(e.ToString());
       Console.WriteLine("Exception of type {0} occurred.", e.GetType());
   }
}

[C++] 
private:
 void DemonstrateEnforceConstraints(){
    // Create a DataSet with one table, one column and a UniqueConstraint.
    DataSet* ds= new DataSet(S"myDataSet");
    DataTable* t = new DataTable(S"myTable");
    DataColumn* c = new DataColumn(S"col1");
    // A UniqueConstraint is added when the Unique property is true.
    c->Unique=true;
    t->Columns->Add(c);
    ds->Tables->Add(t);
    Console::WriteLine(S"constraints.count: {0}", __box(t->Constraints->Count));
    // add five rows.
    DataRow* r ;
    for(int i=0;i<5;i++){
       r = t->NewRow();
       r->Item[S"col1"] = __box(i);
       t->Rows->Add(r);
    }
    t->AcceptChanges();
    
    ds->EnforceConstraints=false;
    // Change the values of all rows to 1.
    System::Collections::IEnumerator* myEnum = t->Rows->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataRow* thisRow = __try_cast<DataRow*>(myEnum->Current);
       thisRow->Item[S"col1"]=__box(1);
       //Console.WriteLine("\t" + thisRow[0]);
    }
    try{
       ds->EnforceConstraints=true;
    }
    catch(System::Data::ConstraintException* e){
    // Process exception and return.
        System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
        log->Source = S"My Application";
        log->WriteEntry(e->ToString());
        Console::WriteLine(S"Exception of type {0} occurred.", e->GetType());
    }
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataSet Class | DataSet Members | System.Data Namespace | Constraints | ConstraintException | ForeignKeyConstraint | DataTable | UniqueConstraint

Show:
© 2014 Microsoft