DataSet.AcceptChanges 方法

定義

認可從載入這個 DataSet 物件或前一次呼叫 AcceptChanges() 以來,對該物件做的所有變更。

public:
 void AcceptChanges();
public void AcceptChanges ();
member this.AcceptChanges : unit -> unit
Public Sub AcceptChanges ()

範例

下列範例會在 DataRowDataTableDataSet將 加入 。 AcceptChanges接著會在上DataSet呼叫 方法,這個方法會串聯至它包含的所有DataTable物件。

private void AcceptChanges()
{
   DataSet myDataSet;
   myDataSet = new DataSet();

   // Not shown: methods to fill the DataSet with data.
   DataTable t;
   t = myDataSet.Tables["Suppliers"];

   // Add a DataRow to a table.
   DataRow myRow;
   myRow = t.NewRow();
   myRow["CompanyID"] = "NWTRADECO";
   myRow["CompanyName"] = "NortWest Trade Company";

   // Add the row.
   t.Rows.Add( myRow );

   // Calling AcceptChanges on the DataSet causes AcceptChanges to be
   // called on all subordinate objects.
   myDataSet.AcceptChanges();
}
Private Sub AcceptChanges()
   Dim myDataSet As DataSet
   myDataSet = new DataSet()

   ' Not shown: methods to fill the DataSet with data.
   Dim t As DataTable

   t = myDataSet.Tables("Suppliers")

   ' Add a DataRow to a table.
   Dim myRow As DataRow
   myRow = t.NewRow()
   myRow("CompanyID") = "NWTRADECO"
   myRow("CompanyName") = "NortWest Trade Company"

   ' Add the row.
   t.Rows.Add( myRow )

   ' Calling AcceptChanges on the DataSet causes AcceptChanges to be
   ' called on all subordinate objects.
   myDataSet.AcceptChanges()
End Sub

備註

DataRowDataTable 類別都有 AcceptChanges 方法。 在DataTable層級呼叫 AcceptChanges 會導致AcceptChanges呼叫每個 DataRow 方法的方法。 同樣地,叫用 AcceptChangesDataSet 的原因 AcceptChanges 會在 內的 DataSet每個數據表上呼叫。 如此一來,您就會有多個層級可以叫用 方法。 AcceptChanges呼叫 的 DataSet 可讓您在所有次級物件上叫用 方法 (,例如數據表和數據列) 一次呼叫。

當您在上DataSet呼叫 AcceptChanges 時,任何DataRow仍在編輯模式中的物件都會順利結束其編輯。 RowState每個 DataRow 屬性也會變更;Added而數據Modified列會變成 Unchanged,而且會移除數據Deleted列。

DataSet如果包含 ForeignKeyConstraint 物件,叫AcceptChanges用 方法也會強制執行 AcceptRejectRule

注意

AcceptChangesRejectChanges 僅適用於 DataRow (,也就是新增、移除、刪除和修改) 。 它們不適用於架構或結構變更。

如果 DataSet 已使用 DataAdapter 填滿,呼叫 AcceptChanges 將不會將這些變更復寫回數據源。 在這裡情況下,請改為呼叫 Update 。 如需詳細資訊,請參閱 使用 DataAdapters 更新數據源

適用於

另請參閱