DataSet.GetChanges DataSet.GetChanges DataSet.GetChanges DataSet.GetChanges Method

Definizione

Ottiene una copia dell'oggetto DataSet contenente tutte le modifiche apportate sin dall'ultimo caricamento o dall'ultima chiamata a AcceptChanges().Gets a copy of the DataSet containing all changes made to it since it was last loaded, or since AcceptChanges() was called.

Overload

GetChanges() GetChanges() GetChanges() GetChanges()

Ottiene una copia della classe DataSet contenente tutte le modifiche apportate a essa dal momento in cui è stata caricata o dall'ultima chiamata al metodo AcceptChanges().Gets a copy of the DataSet that contains all changes made to it since it was loaded or since AcceptChanges() was last called.

GetChanges(DataRowState) GetChanges(DataRowState) GetChanges(DataRowState) GetChanges(DataRowState)

Ottiene una copia della classe DataSet contenente tutte le modifiche apportate a essa dal momento in cui è stata caricata o dall'ultima chiamata al metodo AcceptChanges(), filtrata dall'enumerazione DataRowState.Gets a copy of the DataSet containing all changes made to it since it was last loaded, or since AcceptChanges() was called, filtered by DataRowState.

GetChanges() GetChanges() GetChanges() GetChanges()

Ottiene una copia della classe DataSet contenente tutte le modifiche apportate a essa dal momento in cui è stata caricata o dall'ultima chiamata al metodo AcceptChanges().Gets a copy of the DataSet that contains all changes made to it since it was loaded or since AcceptChanges() was last called.

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

Restituisce

Copia delle modifiche da DataSet su cui è possibile eseguire azioni e che successivamente può essere nuovamente unita tramite Merge(DataSet).A copy of the changes from this DataSet that can have actions performed on it and later be merged back in using Merge(DataSet). Se non vengono trovate righe modificate, il metodo restituisce null.If no changed rows are found, the method returns null.

Esempi

L'esempio seguente crea una semplice DataSet con una tabella, due colonne e dieci righe.The following example creates a simple DataSet with one table, two columns, and ten rows. Due valori vengono modificati, e viene aggiunta una riga.Two values are changed, and one row is added. Viene creato un subset dei dati modificati utilizzando il GetChanges (metodo).A subset of the changed data is created using the GetChanges method. Dopo la risoluzione degli errori, viene aggiunta una nuova colonna al sottoinsieme, modifica dello schema.After reconciling errors, a new column is added to the subset, changing the schema. Quando la Merge metodo viene chiamato con il missingSchemaAction impostata su MissingSchemaAction.Add, la nuova colonna verrà aggiunta all'istanza originale DataSet schema dell'oggetto.When the Merge method is called with the missingSchemaAction set to MissingSchemaAction.Add, the new column is added to the original DataSet object's schema.

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(1) 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

Commenti

Crea un nuovo DataSet che contiene una copia di tutte le righe nell'originale DataSet che con modifiche in sospeso.Creates a new DataSet that contains a copy of all rows in the original DataSet that have pending changes. I vincoli di relazione possono causare altre righe non modificate da aggiungere al nuovo DataSet se tali righe contengono chiavi primarie corrispondenti alle chiavi esterne in righe modificate.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. Il metodo restituisce null se non sono presenti righe nell'originale DataSet che con modifiche in sospeso.The method returns null if there are no rows in the original DataSet that have pending changes.

Vedi anche

GetChanges(DataRowState) GetChanges(DataRowState) GetChanges(DataRowState) GetChanges(DataRowState)

Ottiene una copia della classe DataSet contenente tutte le modifiche apportate a essa dal momento in cui è stata caricata o dall'ultima chiamata al metodo AcceptChanges(), filtrata dall'enumerazione DataRowState.Gets a copy of the DataSet containing all changes made to it since it was last loaded, or since AcceptChanges() was called, filtered by DataRowState.

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

Parametri

rowStates
DataRowState DataRowState DataRowState DataRowState

Uno dei valori di DataRowState.One of the DataRowState values.

Restituisce

Copia filtrata della classe DataSet su cui è possibile eseguire azioni e che successivamente può essere unita di nuovo all'originale utilizzando il metodo Merge(DataSet).A filtered copy of the DataSet that can have actions performed on it, and subsequently be merged back in using Merge(DataSet). Se non viene trovata alcuna riga dell'oggetto DataRowState desiderato, questo metodo restituirà null.If no rows of the desired DataRowState are found, the method returns null.

Esempi

L'esempio seguente usa il GetChanges per creare un secondo metodo DataSet oggetto, che viene quindi usato per aggiornare un'origine dati.The following example uses the GetChanges method to create a second DataSet object, which is then used to update a data source.

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

Commenti

Il GetChanges metodo viene utilizzato per produrre una seconda DataSet oggetto che contiene solo le modifiche apportate all'originale.The GetChanges method is used to produce a second DataSet object that contains only the changes introduced into the original. Usare il rowStates argomento per specificare il tipo di modifiche deve includere il nuovo oggetto.Use the rowStates argument to specify the type of changes the new object should include.

Questo valore restituito copia è progettata per essere nuovamente unita all'oggetto originale DataSet.This returned copy is designed to be merged back in to this original DataSet. I vincoli di relazione possono causare le righe padre contrassegnate Unchanged da includere.Relationship constraints may cause parent rows marked Unchanged to be included. Se nessuna riga dell'oggetto desiderato DataRowState vengono rilevati, il GetChanges restituzione del metodo null.If no rows of the desired DataRowState are found, the GetChanges method returns null.

Vedi anche

Si applica a