提交自上次调用 AcceptChanges 以来对该表进行的所有更改。
[Visual Basic] Public Sub AcceptChanges() [C#] public void AcceptChanges(); [C++] public: void AcceptChanges(); [JScript] public function AcceptChanges();
备注
调用 AcceptChanges 时,任何仍处于编辑模式的 DataRow 对象将成功结束其编辑。DataRowState 也会随之更改:所有状态为 Added 和 Modified 的行的状态都变为 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