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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.