DataSet.AcceptChanges 方法

定义

提交自加载此 DataSet 或上次调用 AcceptChanges() 以来对其进行的所有更改。Commits all the changes made to this DataSet since it was loaded or since the last time AcceptChanges() was called.

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

示例

下面的示例将 DataRow 添加到 DataSet中的 DataTableThe following example adds a DataRow to a DataTable in a DataSet. 然后对 DataSet调用 AcceptChanges 方法,该方法可与它包含的所有 DataTable 对象进行级联。The AcceptChanges method is then called on the DataSet, which cascades to all DataTable objects that it contains.

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 方法。Both the DataRow and DataTable classes have AcceptChanges methods. DataTable 级别调用 AcceptChanges 会导致调用每个 DataRowAcceptChanges 方法。Calling AcceptChanges at the DataTable level causes the AcceptChanges method for each DataRow to be called. 同样,对 DataSet 调用 AcceptChanges 会导致对 DataSet中的每个表调用 AcceptChangesSimilarly, invoking AcceptChanges on the DataSet causes AcceptChanges to be called on each table within the DataSet. 通过这种方式,可以调用方法的多个级别。In this manner, you have multiple levels at which the method can be invoked. 通过调用 DataSetAcceptChanges,你可以通过一个调用对所有从属对象(例如,表和行)调用方法。Calling the AcceptChanges of the DataSet enables you to invoke the method on all subordinate objects (for example, tables and rows) with one call.

DataSet调用 AcceptChanges 时,仍处于编辑模式的任何 DataRow 对象将会成功完成其编辑。When you call AcceptChanges on the DataSet, any DataRow objects still in edit-mode end their edits successfully. 每个 DataRowRowState 属性也会发生变化;AddedModified 行将变为 Unchanged,并且 Deleted 行被删除。The RowState property of each DataRow also changes; Added and Modified rows become Unchanged, and Deleted rows are removed.

如果 DataSet 包含 ForeignKeyConstraint 对象,则调用 AcceptChanges 方法还会导致 AcceptRejectRule 强制执行。If the DataSet contains ForeignKeyConstraint objects, invoking the AcceptChanges method also causes the AcceptRejectRule to be enforced.

备注

AcceptChangesRejectChanges 仅适用于 DataRow 的相关更改(即,添加、删除、删除和修改)。AcceptChanges and RejectChanges only apply to DataRow related changes (that is, Add, Remove, Delete, and Modify). 它们不适用于架构或结构更改。They are not applicable to schema or structural changes.

如果已使用 DataAdapter 填充 DataSet,则调用 AcceptChange 不会将这些更改复制回数据源中。Calling AcceptChanges will not replicate these changes back to the data source if the DataSet was filled using a DataAdapter. 在此情况下,请改为调用 UpdateIn that situation, call Update instead. 有关详细信息,请参阅通过 Dataadapter 更新数据源See Updating Data Sources with DataAdapters for more information.

适用于

另请参阅