Partilhar via


How to: Turn Off Constraints While Filling a Dataset

If a dataset contains constraints (such as a foreign-key constraint) it is possible to cause exceptions to be thrown depending on the order of operations performed against the dataset. For example, loading child records before loading the related parent records can violate the constraint and cause an exception. As soon as you load a child record the constraint checks for the related parent record and raises an error. If there were no mechanism to allow temporary constraint suspension, an error would be raised every time you tried to load a record into the child table. Another way to suspend all constraints in a dataset is with the BeginEdit, and EndEdit properties.

Note

Validation events (for example, ColumnChanging, RowChanging, and so on) will not be raised when constraints are turned off.

To suspend update constraints programmatically

  • The following example shows how to temporarily turn off constraint checking in a dataset:

    DataSet1.EnforceConstraints = False 
    ' Perform some operations on the dataset
    DataSet1.EnforceConstraints = True
    
    dataSet1.EnforceConstraints = false;
    // Perform some operations on the dataset
    dataSet1.EnforceConstraints = true;
    

To suspend update constraints using the Dataset Designer

  1. Open your dataset in the Creating and Editing Typed Datasets. For more information, see How to: Open a Dataset in the Dataset Designer.

  2. Set the EnforceConstraints property to false in the Properties window.

See Also

Concepts

Saving Data in Datasets

Editing Data in Your Application

Binding Windows Forms Controls to Data in Visual Studio

Preparing Your Application to Receive Data

Fetching Data into Your Application

Binding Controls to Data in Visual Studio

Validating Data

Saving Data

Other Resources

Data Walkthroughs

Overview of Data Applications in Visual Studio

Connecting to Data in Visual Studio