DataTable.GetChanges 方法

定义

获取 DataTable 的副本,该副本包含自上次加载以来或自调用 AcceptChanges() 以来的所有更改。Gets a copy of the DataTable containing all changes made to it since it was last loaded, or since AcceptChanges() was called.

重载

GetChanges()

获取 DataTable 的副本,该副本包含自加载以来或自上次调用 AcceptChanges() 以来进行的所有更改。Gets a copy of the DataTable that contains all changes made to it since it was loaded or AcceptChanges() was last called.

GetChanges(DataRowState)

获取由 DataRowState 筛选的 DataTable 的副本,该副本包含上次加载以来或调用 AcceptChanges() 以来进行的所有更改。Gets a copy of the DataTable containing all changes made to it since it was last loaded, or since AcceptChanges() was called, filtered by DataRowState.

GetChanges()

获取 DataTable 的副本,该副本包含自加载以来或自上次调用 AcceptChanges() 以来进行的所有更改。Gets a copy of the DataTable that contains all changes made to it since it was loaded or AcceptChanges() was last called.

public:
 System::Data::DataTable ^ GetChanges();
public System.Data.DataTable? GetChanges ();
public System.Data.DataTable GetChanges ();
member this.GetChanges : unit -> System.Data.DataTable
Public Function GetChanges () As DataTable

返回

DataTable

DataTable 中的更改的副本,如果未发现更改,则为 nullA copy of the changes from this DataTable, or null if no changes are found.

示例

private void UpdateDataTable(DataTable table,
    OleDbDataAdapter myDataAdapter)
{
    DataTable xDataTable = table.GetChanges();

    // Check the DataTable for errors.
    if (xDataTable.HasErrors)
    {
        // Insert code to resolve errors.
    }

    // After fixing errors, update the database with the DataAdapter
    myDataAdapter.Update(xDataTable);
}
Private Sub UpdateDataTable(table As DataTable, _
    myDataAdapter As OleDbDataAdapter)

    Dim xDataTable As DataTable = table.GetChanges()

    ' Check the DataTable for errors.
    If xDataTable.HasErrors Then
        ' Insert code to resolve errors.
    End If

    ' After fixing errors, update the database with the DataAdapter 
    myDataAdapter.Update(xDataTable)
End Sub

注解

创建一个新的 DataSet ,其中包含原始中 DataSet 具有挂起的更改的所有行的副本。Creates a new DataSet containing a copy of all rows in the original DataSet that have pending changes. DataSet如果未更改的行包含与更改的行中的外键相对应的主键,则关系约束可能导致将其他未更改的行添加到新的中。Relationship constraints can cause additional unchanged rows to be added to the new DataSet if the unchanged rows contain primary keys corresponding to foreign keys in the changed rows. null Nothing 如果原始中不存在具有挂起更改的行,则该方法将返回 (Visual Basic) DataSetThe method returns null (Nothing in Visual Basic) if there are no rows in the original DataSet with pending changes.

适用于

GetChanges(DataRowState)

获取由 DataRowState 筛选的 DataTable 的副本,该副本包含上次加载以来或调用 AcceptChanges() 以来进行的所有更改。Gets a copy of the DataTable containing all changes made to it since it was last loaded, or since AcceptChanges() was called, filtered by DataRowState.

public:
 System::Data::DataTable ^ GetChanges(System::Data::DataRowState rowStates);
public System.Data.DataTable? GetChanges (System.Data.DataRowState rowStates);
public System.Data.DataTable GetChanges (System.Data.DataRowState rowStates);
member this.GetChanges : System.Data.DataRowState -> System.Data.DataTable
Public Function GetChanges (rowStates As DataRowState) As DataTable

参数

rowStates
DataRowState

DataRowState 值之一。One of the DataRowState values.

返回

DataTable

DataTable 的筛选副本,可以对该副本执行操作,之后可以使用 DataTable 将其合并回 Merge(DataSet)A filtered copy of the DataTable that can have actions performed on it, and later be merged back in the DataTable using Merge(DataSet). 如果未找到所需 DataRowState 的行,则该方法返回 nullIf no rows of the desired DataRowState are found, the method returns null.

示例

private void ProcessDeletes(DataTable table,
    OleDbDataAdapter adapter)
{
    DataTable changeTable = table.GetChanges(DataRowState.Deleted);

    // Check the DataTable for errors.
    if (changeTable.HasErrors)
    {
        // Insert code to resolve errors.
    }

    // After fixing errors, update the database with the DataAdapter
    adapter.Update(changeTable);
}
Private Sub ProcessDeletes(table As DataTable, _
    adapter As OleDbDataAdapter)

   Dim changeTable As DataTable = table.GetChanges(DataRowState.Deleted)

   ' Check the DataTable for errors.
   If table.HasErrors Then
      ' Insert code to resolve errors.
   End If

   ' After fixing errors, update the database with the DataAdapter 
   adapter.Update(changeTable)
End Sub

注解

GetChanges方法用于生成第二个 DataTable 对象,该对象仅包含在原始中引入的更改。The GetChanges method is used to produce a second DataTable object that contains only the changes introduced into the original. 使用 rowStates 参数可指定新对象应包含的更改类型。Use the rowStates argument to specify the type of changes the new object should include.

关系约束可能会导致包括未更改的父行。Relationship constraints may cause unchanged parent rows to be included.

适用于