DataSet.GetChanges 方法

定义

获取 DataSet 的副本,该副本包含自上次加载以来或自调用 AcceptChanges() 以来的所有更改。

重载

GetChanges()

获取 DataSet 的副本,该副本包含自加载以来或自上次调用 AcceptChanges() 以来对该数据集进行的所有更改。

GetChanges(DataRowState)

获取由 DataRowState 筛选的 DataSet 的副本,该副本包含上次加载以来或调用 AcceptChanges() 以来进行的所有更改。

GetChanges()

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

获取 DataSet 的副本,该副本包含自加载以来或自上次调用 AcceptChanges() 以来对该数据集进行的所有更改。

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

返回

DataSet 中更改的副本,可以对该副本执行操作,然后可以使用 Merge(DataSet) 将其合并回去。 如果未找到更改的行,则该方法将返回 null

示例

以下示例创建一个包含一个表、两列和十行的简单 DataSet 。 更改了两个值,并添加了一行。 已更改数据的子集是使用 GetChanges 方法创建的。 协调错误后,会将新列添加到子集,从而更改架构。 Merge当调用 方法并将 missingSchemaAction 设置为 MissingSchemaAction.Add时,会将新列添加到原始DataSet对象的架构中。

private void DemonstrateMerge()
{
    // Create a DataSet with one table, two columns,
    // and three rows.
    DataSet dataSet = new DataSet("dataSet");
    DataTable table = new DataTable("Items");
    DataColumn idColumn = new DataColumn("id",
        Type.GetType("System.Int32"),"");
    idColumn.AutoIncrement=true;
    DataColumn itemColumn = new DataColumn("Item",
        Type.GetType("System.Int32"),"");

    // DataColumn array to set primary key.
    DataColumn[] keyColumn= new DataColumn[1];
    DataRow row;

    // Create variable for temporary DataSet.
    DataSet changesDataSet;

    // Add RowChanged event handler for the table.
    table.RowChanged+=new DataRowChangeEventHandler(
        Row_Changed);
    dataSet.Tables.Add(table);
    table.Columns.Add(idColumn);
    table.Columns.Add(itemColumn);

    // Set primary key column.
    keyColumn[0]= idColumn;
    table.PrimaryKey=keyColumn;
    // Add ten rows.
    for(int i = 0; i <10;i++)
    {
        row=table.NewRow();
        row["Item"]= i;
        table.Rows.Add(row);
    }
    // Accept changes.
    dataSet.AcceptChanges();
    PrintValues(dataSet, "Original values");

    // Change row values.
    table.Rows[0]["Item"]= 50;
    table.Rows[1]["Item"]= 111;

    // Add one row.
    row=table.NewRow();
    row["Item"]=74;
    table.Rows.Add(row);

    // Insert code for error checking. Set one row in error.
    table.Rows[1].RowError= "over 100";
    PrintValues(dataSet, "Modified and New Values");

    // If the table has changes or errors,
    // create a subset DataSet.
    if(dataSet.HasChanges(DataRowState.Modified |
        DataRowState.Added)&& dataSet.HasErrors)
    {
        // Use GetChanges to extract subset.
        changesDataSet = dataSet.GetChanges(
            DataRowState.Modified|DataRowState.Added);
        PrintValues(changesDataSet, "Subset values");

        // Insert code to reconcile errors. In this case, reject changes.
        foreach(DataTable changesTable in changesDataSet.Tables)
        {
            if (changesTable.HasErrors)
            {
                foreach(DataRow changesRow in changesTable.Rows)
                {
                    //Console.WriteLine(changesRow["Item"]);
                    if((int)changesRow["Item",DataRowVersion.Current ]> 100)
                    {
                        changesRow.RejectChanges();
                        changesRow.ClearErrors();
                    }
                }
            }
        }
        // Add a column to the changesDataSet.
        changesDataSet.Tables["Items"].Columns.Add(
            new DataColumn("newColumn"));
        PrintValues(changesDataSet, "Reconciled subset values");
        // Merge changes back to first DataSet.
        dataSet.Merge(changesDataSet, false,
            System.Data.MissingSchemaAction.Add);
    }
    PrintValues(dataSet, "Merged Values");
}

private void Row_Changed(object sender, DataRowChangeEventArgs e)
{
    Console.WriteLine("Row Changed " + e.Action.ToString()
        + "\table" + e.Row.ItemArray[0]);
}

private void PrintValues(DataSet dataSet, string label)
{
    Console.WriteLine(label + "\n");
    foreach(DataTable table in dataSet.Tables)
    {
        Console.WriteLine("TableName: " + table.TableName);
        foreach(DataRow row in table.Rows)
        {
            foreach(DataColumn column in table.Columns)
            {
                Console.Write("\table " + row[column] );
            }
            Console.WriteLine();
        }
    }
}
Private Sub DemonstrateMerge()
    ' Create a DataSet with one table, two columns, 
    ' and three rows.
    Dim dataSet As New DataSet("dataSet")
    Dim table As New DataTable("Items")
    Dim idColumn As New DataColumn("id", _
        Type.GetType("System.Int32"), "")
    idColumn.AutoIncrement = True
    Dim itemColumn As New DataColumn("Item", _
        Type.GetType("System.Int32"), "")

    ' Create DataColumn array to set primary key.
    Dim keyColumn(0) As DataColumn
    Dim row As DataRow

    ' Create variable for temporary DataSet. 
    Dim changesDataSet As DataSet

    ' Add RowChanged event handler for the table.
    AddHandler table.RowChanged, AddressOf Row_Changed
    dataSet.Tables.Add(table)
    table.Columns.Add(idColumn)
    table.Columns.Add(itemColumn)

    ' Set primary key column.
    keyColumn(0) = idColumn
    table.PrimaryKey = keyColumn

    ' Add ten rows.
    Dim i As Integer
    For i = 0 To 9
        row = table.NewRow()
        row("Item") = i
        table.Rows.Add(row)
    Next i

    ' Accept changes.
    dataSet.AcceptChanges()
    PrintValues(dataSet, "Original values")

    ' Change row values.
    table.Rows(0)("Item") = 50
    table.Rows(1)("Item") = 111

    ' Add one row.
    row = table.NewRow()
    row("Item") = 74
    table.Rows.Add(row)

    ' Insert code for error checking. Set one row in error.
    table.Rows(1).RowError = "over 100"
    PrintValues(dataSet, "Modified and New Values")

    ' If the table has changes or errors, create a subset DataSet.
    If dataSet.HasChanges(DataRowState.Modified Or DataRowState.Added) _
        And dataSet.HasErrors Then
        ' Use GetChanges to extract subset.
        changesDataSet = dataSet.GetChanges( _
            DataRowState.Modified Or DataRowState.Added)
        PrintValues(changesDataSet, "Subset values")

        ' Insert code to reconcile errors. In this case, reject changes.
        Dim changesTable As DataTable
        For Each changesTable In  changesDataSet.Tables
            If changesTable.HasErrors Then
                Dim changesRow As DataRow
                For Each changesRow In  changesTable.Rows
                    'Console.WriteLine(changesRow["Item"]);
                    If CInt(changesRow("Item", _
                        DataRowVersion.Current)) > 100 Then
                        changesRow.RejectChanges()
                        changesRow.ClearErrors()
                    End If
                Next changesRow
            End If
        Next changesTable

        ' Add a column to the changesDataSet.
        changesDataSet.Tables("Items").Columns.Add( _
            New DataColumn("newColumn"))
        PrintValues(changesDataSet, "Reconciled subset values")

        ' Merge changes back to first DataSet.
        dataSet.Merge(changesDataSet, False, _
            System.Data.MissingSchemaAction.Add)
    End If
    PrintValues(dataSet, "Merged Values")
End Sub
        
 Private Sub Row_Changed(sender As Object, e As DataRowChangeEventArgs)
     Console.WriteLine("Row Changed " + e.Action.ToString() _
        + ControlChars.Tab + e.Row.ItemArray(0).ToString())
 End Sub
    
Private Sub PrintValues(dataSet As DataSet, label As String)
     Console.WriteLine(label + ControlChars.Cr)
     Dim table As DataTable
     For Each table In  dataSet.Tables
         Console.WriteLine("TableName: " + table.TableName)
         Dim row As DataRow
         For Each row In  table.Rows
             Dim column As DataColumn
             For Each column In  table.Columns
                 Console.Write(ControlChars.Tab & " " _
                    & row(column).ToString())
             Next column
             Console.WriteLine()
         Next row
     Next table
End Sub

注解

创建一个新的 DataSet ,其中包含原始 DataSet 中具有挂起更改的所有行的副本。 如果未更改的行包含与更改行中的外键对应的主键,关系约束可能会导致将其他未更改的行添加到新的 DataSet 。 如果原始 DataSet 中没有具有挂起更改的行,则方法返回 null

另请参阅

适用于

GetChanges(DataRowState)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

获取由 DataRowState 筛选的 DataSet 的副本,该副本包含上次加载以来或调用 AcceptChanges() 以来进行的所有更改。

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

参数

rowStates
DataRowState

DataRowState 值之一。

返回

DataSet 的筛选副本,可以对该副本执行操作,然后使用 Merge(DataSet) 将其合并回数据集。 如果未找到所需 DataRowState 的行,则该方法返回 null

示例

下面的示例使用 GetChanges 方法创建第二 DataSet 个 对象,然后使用该对象更新数据源。

private void UpdateDataSet(DataSet dataSet)
{
    // Check for changes with the HasChanges method first.
    if(!dataSet.HasChanges(DataRowState.Modified)) return;

    // Create temporary DataSet variable and
    // GetChanges for modified rows only.
    DataSet tempDataSet =
        dataSet.GetChanges(DataRowState.Modified);

    // Check the DataSet for errors.
    if(tempDataSet.HasErrors)
    {
        // Insert code to resolve errors.
    }
    // After fixing errors, update the data source with
    // the DataAdapter used to create the DataSet.
    adapter.Update(tempDataSet);
}
Private Sub UpdateDataSet(ByVal dataSet As DataSet)
   ' Check for changes with the HasChanges method first.
   If Not dataSet.HasChanges(DataRowState.Modified) Then 
       Exit Sub
   End If

   ' Create temporary DataSet variable and
   ' GetChanges for modified rows only.
   Dim tempDataSet As DataSet = _
       dataSet.GetChanges(DataRowState.Modified)

   ' Check the DataSet for errors.
   If tempDataSet.HasErrors Then
      ' Insert code to resolve errors.
   End If

   ' After fixing errors, update the data source with   
   ' the DataAdapter used to create the DataSet.
   adapter.Update(tempDataSet)
End Sub

注解

方法 GetChanges 用于生成第二 DataSet 个对象,该对象仅包含引入原始的更改。 rowStates使用 参数指定新对象应包含的更改的类型。

此返回的副本旨在合并回此原始 DataSet。 关系约束可能会导致包含标记 Unchanged 的父行。 如果未找到所需的 DataRowState 行,该方法 GetChanges 将返回 null

另请参阅

适用于