DataTable.AcceptChanges 方法

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

**命名空间:**System.Data
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public Sub AcceptChanges
用法
Dim instance As DataTable

instance.AcceptChanges
public void AcceptChanges ()
public:
void AcceptChanges ()
public void AcceptChanges ()
public function AcceptChanges ()

备注

调用 AcceptChanges 时,任何仍处于编辑模式的 DataRow 对象将成功结束其编辑。DataRowState 也发生更改:所有 AddedModified 行成为 UnchangedDeleted 行被移除。

在您尝试使用 DbDataAdapter.Update 方法更新 DataSet 之后,通常会对 DataTable 调用 AcceptChanges 方法。

示例

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

Private Sub AcceptOrReject(table As DataTable)
    ' If there are errors, try to reconcile.
    If( Not table.HasErrors) 
       If(Reconcile(table))
          ' 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
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;
}

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

DataTable 类
DataTable 成员
System.Data 命名空间

其他资源

创建和使用 DataTables