Use a Transaction with the DataAdapter

Problem

You want to ensure that DataAdapter changes are committed or rolled back as a whole.

Solution

Create a Transaction object, and assign it the DataAdapter commands that are used for deleting, updating, and inserting records.

Discussion

The DataAdapter does not expose a Transaction property. However, the Command objects that the DataAdapter uses do provide this property. The following application shows how you can ensure that all commands in a DataAdapter update operation are grouped into a single transaction:

Dim Con As New SqlConnection(ConnectionString) Dim CmdSelect As New SqlCommand("SELECT * FROM Shippers", Con) Dim Ds As New DataSet() Dim Adapter As New SqlDataAdapter(CmdSelect) ' (Define the custom ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.