Breyta

Deila með


DataAdapter.AcceptChangesDuringUpdate Property

Definition

Gets or sets whether AcceptChanges() is called during a Update(DataSet).

public:
 property bool AcceptChangesDuringUpdate { bool get(); void set(bool value); };
public bool AcceptChangesDuringUpdate { get; set; }
member this.AcceptChangesDuringUpdate : bool with get, set
Public Property AcceptChangesDuringUpdate As Boolean

Property Value

true if AcceptChanges() is called during an Update(DataSet); otherwise false. The default is true.

Examples

This example demonstrates extracting changed rows from a DataTable and using a SqlDataAdapter to update the data source and retrieve a new identity column value. By setting the AcceptChangesDuringUpdate property of the SqlDataAdapter to false to preserve the original auto increment value, the new data can then be merged into the original DataTable, even if the new identity value does not match the original auto increment value in the DataTable.

private static void MergeIdentityColumns(string connectionString)
{
    using (SqlConnection connection =
               new SqlConnection(connectionString))
    {
        // Create the DataAdapter
        SqlDataAdapter adapter =
            new SqlDataAdapter(
            "SELECT ShipperID, CompanyName FROM dbo.Shippers",
            connection);

        //Add the InsertCommand to retrieve new identity value.
        adapter.InsertCommand = new SqlCommand(
            "INSERT INTO dbo.Shippers (CompanyName) " +
            "VALUES (@CompanyName); " +
            "SELECT ShipperID, CompanyName FROM dbo.Shippers " +
            "WHERE ShipperID = SCOPE_IDENTITY();", connection);

        // Set AcceptChangesDuringUpdate to false
        adapter.AcceptChangesDuringUpdate = false;

        // Add the parameter for the inserted value.
        adapter.InsertCommand.Parameters.Add(
           new SqlParameter("@CompanyName", SqlDbType.NVarChar, 40,
           "CompanyName"));
        adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.Both;

        // MissingSchemaAction adds any missing schema to
        // the DataTable, including auto increment columns
        adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

        // Fill a DataTable.
        DataTable shipper = new DataTable();
        adapter.Fill(shipper);

        // Add a new shipper row.
        DataRow newRow = shipper.NewRow();
        newRow["CompanyName"] = "New Shipper";
        shipper.Rows.Add(newRow);

        // Add changed rows to a new DataTable. This
        // DataTable will be used to update the data source.
        DataTable dataChanges = shipper.GetChanges();

        adapter.Update(dataChanges);
        connection.Close();

        Console.WriteLine("Rows after merge.");
        foreach (DataRow rowBefore in shipper.Rows)
        {
            {
                Console.WriteLine("{0}: {1}", rowBefore[0], rowBefore[1]);
            }
        }

        // Merge the two DataTables to get new values.
        shipper.Merge(dataChanges);

        // Commit the changes.
        shipper.AcceptChanges();

        Console.WriteLine("Rows after merge.");
        foreach (DataRow rowAfter in shipper.Rows)
        {
            {
                Console.WriteLine("{0}: {1}", rowAfter[0], rowAfter[1]);
            }
        }
    }
}
Private Sub MergeIdentityColumns(ByVal connectionString As String)

    Using connection As SqlConnection = New SqlConnection( _
       connectionString)

        ' Create the DataAdapter
        Dim adapter As SqlDataAdapter = New SqlDataAdapter( _
          "SELECT ShipperID, CompanyName FROM dbo.Shippers", connection)

        ' Add the InsertCommand to retrieve new identity value.
        adapter.InsertCommand = New SqlCommand( _
            "INSERT INTO dbo.Shippers (CompanyName) " & _
            "VALUES (@CompanyName); " & _
            "SELECT ShipperID, CompanyName FROM dbo.Shippers " & _
            "WHERE ShipperID = SCOPE_IDENTITY();", _
            connection)

        ' Set AcceptChangesDuringUpdate to false.
        adapter.AcceptChangesDuringUpdate = False

        ' Add the parameter for the inserted value.
        adapter.InsertCommand.Parameters.Add( _
           New SqlParameter("@CompanyName", SqlDbType.NVarChar, 40, _
           "CompanyName"))
        adapter.InsertCommand.UpdatedRowSource = _
           UpdateRowSource.FirstReturnedRecord

        ' MissingSchemaAction adds any missing schema to 
        ' the DataTable, including auto increment columns
        adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

        ' Fill a DataTable.
        Dim shipper As New DataTable
        adapter.Fill(shipper)

        ' Add a new shipper row. 
        Dim newRow As DataRow = shipper.NewRow()
        newRow("CompanyName") = "New Shipper"
        shipper.Rows.Add(newRow)

        ' Add changed rows to a new DataTable. This
        ' DataTable will be used to update the data source.
        Dim dataChanges As DataTable = shipper.GetChanges()

        ' Update the data source with the modified records.
        adapter.Update(dataChanges)

        Console.WriteLine("Rows before merge.")
        Dim rowBefore As DataRow
        For Each rowBefore In shipper.Rows
            Console.WriteLine("{0}: {1}", rowBefore(0), rowBefore(1))
        Next

        ' Merge the two DataTables to get new values.
        shipper.Merge(dataChanges)

        ' Commit the changes.
        shipper.AcceptChanges()

        Console.WriteLine("Rows after merge.")
        Dim rowAfter As DataRow
        For Each rowAfter In shipper.Rows
            Console.WriteLine("{0}: {1}", rowAfter(0), rowAfter(1))
        Next
    End Using
End Sub

Remarks

During a call to the Update method of a DataAdapter, the database can send data back to your ADO.NET application as output parameters or as the first returned record of a result set. ADO.NET can retrieve these values and update the corresponding columns in the DataRow being updated. By default, ADO.NET calls the AcceptChanges method of the DataRow after the update. However, if you want to merge the updated row back into another DataTable, you may want to preserver the original value of a primary key column. For example, a primary key column corresponding to an automatically incrementing column in the database, such as an identity column, can contain new values that are assigned by the database that do not match the original values assigned in the DataRow. By default, AcceptChanges is called implicitly after an update, and the original values in the row, which may have been AutoIncrement values assigned by ADO.NET, are lost. You can preserve the original values in the DataRow by preventing ADO.NET from calling AcceptChanges after it performs an update on a row, by setting the AcceptChangesDuringUpdate property to false, which preserves the original values.

Note

Setting the AcceptChangesDuringUpdate property to false applies to all data modifications, not only inserts. If you want to edit or delete rows in the same update, and if you want to suppress the call to AcceptChanges only for inserts, then instead of setting AcceptChangesDuringUpdate to false, use an event handler for the RowUpdated event of the DataAdapter. In the event handler, you can check the StatementType to determine if the data modification is an insert, and if true, set the Status property of the RowUpdatedEventArgs to SkipCurrentRow. For more information and an example, see Retrieving Identity or Autonumber Values.

Applies to

See also