DataTable.AcceptChanges 方法
本文內容
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
認可自前一次呼叫 AcceptChanges() 以來對這個資料表所做的所有變更。
public:
void AcceptChanges();
public void AcceptChanges ();
member this.AcceptChanges : unit -> unit
Public Sub AcceptChanges ()
下列範例會測試每個數據表是否有錯誤。 如果資料表的錯誤可以藉由將它傳遞至未定義的函式) 來協調 (, AcceptChanges 則會呼叫 ,否則 RejectChanges 會呼叫 。
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
呼叫 時 AcceptChanges , DataRow 任何仍在編輯模式中的物件都已成功結束其編輯。
DataRowState也會變更:所有數據AddedModified列都會變成 Unchanged,而且Deleted會移除數據列。
AcceptChanges當您嘗試使用 DbDataAdapter.Update 方法更新 DataSet 之後,通常會在上DataTable呼叫 方法。
| 產品 | 版本 |
|---|---|
| .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 |