DataTable.AcceptChanges Method

Definition

提交自上次调用 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 ()

Examples

下面的示例将测试每个表中的错误。The following example tests each table for errors. 如果可以对表的错误进行协调(通过将其传递到未定义的函数),则调用 AcceptChanges;否则,将调用 RejectChangesIf 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

Remarks

调用 AcceptChanges 时,仍处于编辑模式的任何 DataRow 对象都将成功结束其编辑。When AcceptChanges is called, any DataRow object still in edit mode successfully ends its edits. DataRowState 还会发生更改:所有 AddedModified 行都将变为 Unchanged,并删除 Deleted 行。The DataRowState also changes: all Added and Modified rows become Unchanged, and Deleted rows are removed.

尝试使用 DbDataAdapter.Update 方法更新 DataSet 后,通常会在 DataTable 上调用 AcceptChanges 方法。The AcceptChanges method is generally called on a DataTable after you attempt to update the DataSet using the DbDataAdapter.Update method.

Applies to

See also