DataTable.AcceptChanges 方法

定義

將自上次 AcceptChanges() 呼叫以來所有變更提交到此表。

public:
 void AcceptChanges();
public void AcceptChanges();
member this.AcceptChanges : unit -> unit
Public Sub AcceptChanges ()

範例

以下範例會測試每個資料表是否有錯誤。 若資料表的錯誤可調和(透過傳給未定義函式), AcceptChanges 則呼叫;否則, RejectChanges 呼叫。

private void AcceptOrReject(DataTable table)
{
    // If there are errors, try to reconcile.
    if(table.HasErrors)
    {
        if(Reconcile(table))
        {
            // Fixed all errors.
            table.AcceptChanges();
        }
        else
        {
            // Couldn'table fix all errors.
            table.RejectChanges();
        }
    }
    else
    {
        // If no errors, AcceptChanges.
        table.AcceptChanges();
    }
}

private bool Reconcile(DataTable thisTable)
{
    foreach(DataRow row 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(row.HasErrors)
            return false;
    }
    return true;
}
Private Sub AcceptOrReject(table As DataTable)
    ' If there are errors, try to reconcile.
        If (table.HasErrors) Then
            If (Reconcile(table)) Then
                ' Fixed all errors.
                table.AcceptChanges()
            Else
                ' Couldn'table fix all errors.
                table.RejectChanges()
            End If
        Else
            ' If no errors, AcceptChanges.
            table.AcceptChanges()
        End If
 End Sub
 
Private Function Reconcile(thisTable As DataTable) As Boolean
    Dim row As DataRow
    For Each row 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 row.HasErrors Then
           Reconcile = False
           Exit Function
       End If
    Next row
    Reconcile = True
 End Function

備註

AcceptChanges 被呼叫時,任何 DataRow 仍在編輯模式的物件都能成功結束其編輯。 也 DataRowState 改變了:所有 AddedModified 行變成 Unchanged,且 Deleted 行被移除。

AcceptChanges通常在你嘗試用該DbDataAdapter.Update方法更新後DataSet,會呼叫DataTable這個方法。

適用於

另請參閱