次の方法で共有


DataTable.AcceptChanges メソッド

前回 AcceptChanges を呼び出した以降にこのテーブルに対して行われたすべての変更をコミットします。

Public Sub AcceptChanges()
[C#]
public void AcceptChanges();
[C++]
public: void AcceptChanges();
[JScript]
public function AcceptChanges();

解説

AcceptChanges が呼び出されると、編集モードの DataRow オブジェクトは、正常に編集を終了します。 DataRowState も変更されます。 Added および Modified であるすべての行は Unchanged になり、 Deleted の行は削除されます。

通常、 DbDataAdapter.Update メソッドを使用して DataSet を更新しようとした後に、AcceptChanges メソッドが DataTable で呼び出されます。

使用例

[Visual Basic, C#, C++] 各テーブルにエラーがあるかどうかを確認する例を次に示します。テーブルのエラーを (未定義関数にエラーを渡して) 調整できる場合は AcceptChanges が呼び出されます。それ以外の場合は RejectChanges が呼び出されます。

 
Private Sub AcceptOrReject(myTable As DataTable)
    ' If there are errors, try to reconcile.
    If( Not myTable.HasErrors) 
       If(Reconcile(myTable))
          ' Fixed all errors.
          myTable.AcceptChanges
       Else
         ' Couldn't fix all errors.
          myTable.RejectChanges
       End If
    Else
       ' If no errors, AcceptChanges.
       myTable.AcceptChanges()
    End If
 End Sub
 
Private Function Reconcile(thisTable As DataTable) As Boolean
    Dim myRow As DataRow
    For Each myRow in thisTable.Rows
       'Insert code to try to reconcile error.

       ' If there are still errors return immediately
       ' since the caller rejects all changes upon error.
       If myRow.HasErrors Then
           Reconcile = False
           Exit Function
       End If
    Next myRow
    Reconcile = True
 End Function

[C#] 
private void AcceptOrReject(DataTable myTable)
{
    // If there are errors, try to reconcile.
    if(!myTable.HasErrors)
    { 
       if(Reconcile(myTable))
       {
          // Fixed all errors.
          myTable.AcceptChanges();
       }
       else
       {
          // Couldn't fix all errors.
          myTable.RejectChanges();
       }
    }
    else
       // If no errors, AcceptChanges.
       myTable.AcceptChanges();
}
 
private bool Reconcile(DataTable thisTable)
{
    foreach(DataRow myRow in thisTable.Rows)
    {
       //Insert code to try to reconcile error.

       // If there are still errors return immediately
       // since the caller rejects all changes upon error.
       if(myRow.HasErrors)
           return false;
    }
    return true;
 }

[C++] 
private:
void AcceptOrReject(DataTable* myTable)
{
    // If there are errors, try to reconcile.
    if(!myTable->HasErrors)
    { 
       if(Reconcile(myTable))
       {
          // Fixed all errors.
          myTable->AcceptChanges();
       }
       else
       {
          // Couldn't fix all errors.
          myTable->RejectChanges();
       }
    }
    else
       // If no errors, AcceptChanges.
       myTable->AcceptChanges();
}
 
bool Reconcile(DataTable* thisTable)
{
    System::Collections::IEnumerator* myEnum = thisTable->Rows->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataRow* myRow = __try_cast<DataRow*>(myEnum->Current);
       //Insert code to try to reconcile error.

       // If there are still errors return immediately
       // since the caller rejects all changes upon error.
       if(myRow->HasErrors)
           return false;
    }
    return true;
 }

[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

参照

DataTable クラス | DataTable メンバ | System.Data 名前空間 | AcceptChanges | BeginEdit | DataRowState | EndEdit | RejectChanges