DataTable.AcceptChanges 方法

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

[Visual Basic]
Public Sub AcceptChanges()
[C#]
public void AcceptChanges();
[C++]
public: void AcceptChanges();
[JScript]
public function AcceptChanges();
备注

调用 AcceptChanges 时,任何仍处于编辑模式的 DataRow 对象将成功结束其编辑。DataRowState 也会随之更改:所有状态为 AddedModified 的行的状态都变为 Unchanged;状态为 Deleted 的行则被移除。

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

示例

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

[Visual Basic] 
Private Sub AcceptOrReject(myTable As DataTable)
    ' If there are errors, try to reconcile.
    If( Not myTable.HasErrors) 
       If(Reconcile(myTable))
          ' Fixed all errors.
          myTable.AcceptChanges
       Else
         ' Couldn't fix all errors.
          myTable.RejectChanges
       End If
    Else
       ' If no errors, AcceptChanges.
       myTable.AcceptChanges()
    End If
 End Sub
 
Private Function Reconcile(thisTable As DataTable) As Boolean
    Dim myRow As DataRow
    For Each myRow 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 myRow.HasErrors Then
           Reconcile = False
           Exit Function
       End If
    Next myRow
    Reconcile = True
 End Function

[C#] 
private void AcceptOrReject(DataTable myTable)
{
    // If there are errors, try to reconcile.
    if(!myTable.HasErrors)
    { 
       if(Reconcile(myTable))
       {
          // Fixed all errors.
          myTable.AcceptChanges();
       }
       else
       {
          // Couldn't fix all errors.
          myTable.RejectChanges();
       }
    }
    else
       // If no errors, AcceptChanges.
       myTable.AcceptChanges();
}
 
private bool Reconcile(DataTable thisTable)
{
    foreach(DataRow myRow 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(myRow.HasErrors)
           return false;
    }
    return true;
 }

[C++] 
private:
void AcceptOrReject(DataTable* myTable)
{
    // If there are errors, try to reconcile.
    if(!myTable->HasErrors)
    { 
       if(Reconcile(myTable))
       {
          // Fixed all errors.
          myTable->AcceptChanges();
       }
       else
       {
          // Couldn't fix all errors.
          myTable->RejectChanges();
       }
    }
    else
       // If no errors, AcceptChanges.
       myTable->AcceptChanges();
}
 
bool Reconcile(DataTable* thisTable)
{
    System::Collections::IEnumerator* myEnum = thisTable->Rows->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataRow* myRow = __try_cast<DataRow*>(myEnum->Current);
       //Insert code to try to reconcile error.

       // If there are still errors return immediately
       // since the caller rejects all changes upon error.
       if(myRow->HasErrors)
           return false;
    }
    return true;
 }

[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 语言筛选器

要求

平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版

请参见

DataTable 类 | DataTable 成员 | System.Data 命名空间 | AcceptChanges | BeginEdit | DataRowState | EndEdit | RejectChanges

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值