Export (0) Print
Expand All

DataAdapter.AcceptChangesDuringUpdate Property

Gets or sets whether AcceptChanges is called during a Update.

Namespace:  System.Data.Common
Assembly:  System.Data (in System.Data.dll)

public bool AcceptChangesDuringUpdate { get; set; }

Property Value

Type: System.Boolean
true if AcceptChanges is called during an Update; otherwise false. The default is true.

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.

NoteNote

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 AccceptChangesDuringUpdate 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.

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]);
            }
        }
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft