次の方法で共有


DataSet.EnforceConstraints プロパティ

更新操作を試みたときに操作が制約規則に従っているかどうかを示す値を取得または設定します。

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);

プロパティ値

規則が適用されている場合は true 。それ以外の場合は false 。既定値は true です。

例外

例外の種類 条件
ConstraintException 1 つ以上の制約を適用できません。

解説

制約は、 DataTable レベル (Constraints プロパティ) で設定されます。制約の作成に関する詳細については、「 テーブルへの制約の追加 」を参照してください。

使用例

[Visual Basic, C#, C++] 1 つのテーブル、1 列、5 行、および 1 つの UniqueConstraintDataSet を作成する例を次に示します。 EnforceConstraints プロパティが false に設定され、各行の値が同じ値に設定されています。 EnforceConstraints プロパティを true にリセットすると、 ConstraintException が生成されます。

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

DataSet クラス | DataSet メンバ | System.Data 名前空間 | Constraints | ConstraintException | ForeignKeyConstraint | DataTable | UniqueConstraint