DataTable.AcceptChanges 方法

定义

提交自上次调用 AcceptChanges() 以来对该表进行的所有更改。

C#
public void AcceptChanges ();

示例

以下示例测试每个表的错误。 如果表的错误可以通过将表传递到未定义的函数) 来协调 (, AcceptChanges 则调用 ;否则调用 RejectChanges

C#
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;
}

注解

调用 时 AcceptChanges ,仍处于编辑模式的任何 DataRow 对象将成功完成其编辑。 DataRowState也会更改:所有 AddedModified 行都变为 Unchanged,行Deleted将被删除。

AcceptChanges尝试使用 方法更新 DataSet 后,通常会在 DbDataAdapter.UpdateDataTable调用 方法。

适用于

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另请参阅