填入資料集時關閉條件約束Turn off constraints while filling a dataset

如果資料集包含 (之類的條件約束,例如外鍵條件約束) ,它們可能會引發與針對資料集執行之作業順序相關的錯誤。If a dataset contains constraints (such as foreign-key constraints), they can raise errors related to the order of operations that are performed against the dataset. 例如,載入相關的父記錄之前載入子記錄可能會違反條件約束,並導致錯誤。For example, loading child records before loading related parent records can violate a constraint and cause an error. 一旦載入子記錄,條件約束就會檢查相關的父記錄,並引發錯誤。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. 另一個暫停資料集中之條件約束的方法是使用 BeginEdit 、和 EndEdit 屬性。Another way to suspend all constraints in a dataset is with the BeginEdit, and EndEdit properties.

注意

例如, ColumnChanging RowChanging 在關閉條件約束時,將不會引發 (的驗證事件和) 。Validation events (for example, ColumnChanging and RowChanging) will not be raised when constraints are turned off.

以程式設計方式暫停 update 條件約束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
    

使用 DataSet 設計工具暫止更新條件約束To suspend update constraints using the Dataset Designer

  1. 在 [DataSet 設計工具] 中開啟資料集。Open your dataset in the Dataset Designer. 如需詳細資訊,請參閱 逐步解說:在 DataSet 設計工具中建立資料集For more information, see Walkthrough: Creating a dataset in the Dataset Designer.

  2. 在 [屬性] 視窗中,將 EnforceConstraints 屬性設定為 falseIn the Properties window, set the EnforceConstraints property to false.

另請參閱See also