DataTable.AcceptChanges DataTable.AcceptChanges DataTable.AcceptChanges DataTable.AcceptChanges Method

定義

認可自前一次呼叫 AcceptChanges() 以來對這個資料表所做的所有變更。Commits all the changes made to this table since the last time AcceptChanges() was called.

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

範例

下列範例會測試每個資料表是否有錯誤。The following example tests each table for errors. 如果資料表的錯誤可以進行協調(藉由將它傳遞至未定義的函AcceptChanges式),則會RejectChanges呼叫,否則會呼叫。If the table's errors can be reconciled (by passing it to an undefined function), AcceptChanges is called; otherwise, RejectChanges is called.

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處於編輯模式的任何物件都會成功結束其編輯。When AcceptChanges is called, any DataRow object still in edit mode successfully ends its edits. Modified Unchanged Deleted也會變更:所有Added和資料列都會變成,而且會移除資料列。 DataRowStateThe DataRowState also changes: all Added and Modified rows become Unchanged, and Deleted rows are removed.

在您嘗試DataTable AcceptChanges 使用DbDataAdapter.Update方法更新之後,通常會在上呼叫方法。 DataSetThe AcceptChanges method is generally called on a DataTable after you attempt to update the DataSet using the DbDataAdapter.Update method.

適用於

另請參閱