Listing 1: Visual Basic Command Window Project 'These code examples are provided in the context of a Visual Basic Command Window ' project. To run this sample code, create a new Command Window project in Visual ' Studio and add the references shown in the imports statements below to your project. ' Then paste this code into the Module1.vb source file. (Note that hard returns in the currently wrapping lines will cause errors when pasting.) Imports System.Transactions Imports System.Data.SqlClient Module Module1 Sub Main() ' Uncomment the following line to set My.User to the currently logged on Windows user. ' My.User.InitializeWithWindowsUser BEGIN CALLOUT A Dim myTran As New System.Transactions.CommittableTransaction() Dim myConn As New System.Data.SqlClient.SqlConnection ("server=(local);database=AdventureWorks;Integrated Security=true") Try Dim myCommand As SqlCommand = myConn.CreateCommand() myCommand.CommandText = "usp_Product_SelectPrice" myCommand.CommandType = CommandType.StoredProcedure Dim updateCommand4 As SqlCommand = myConn.CreateCommand() myConn.Open() myConn.EnlistTransaction(myTran) Dim myReader As SqlDataReader = myCommand.ExecuteReader() 'Loop through each item in the reader and increase the price by 20% + $1.00 While myReader.Read() DoCommand(updateCommand4, myReader.GetInt32(0), _ (myReader.GetSqlMoney(1) * 0.2) + 1) End While myReader.Close() myTran.Commit() Catch ex As Exception ' An error means the transaction can't commit myTran.Rollback() Finally myConn.Close() End Try END CALLOUT A BEGIN CALLOUT B Dim myCommitableTran As New System.Transactions.CommittableTransaction() Dim myConnection As New System.Data.SqlClient.SqlConnection("server=(local);database=AdventureWorks;Integrated Security=true") Try myConnection.Open() myConnection.EnlistTransaction(myCommitableTran) Dim updateCommand As SqlCommand = myConnection.CreateCommand() DoCommand(updateCommand, 444, 0.54) myCommitableTran.Commit() Catch ex As Exception ' An error means the transaction can't commit myCommitableTran.Rollback() Finally myConnection.Close() End Try END CALLOUT B BEGIN CALLOUT C Using myTranScope As New System.Transactions.TransactionScope() Dim myConnection2 As New System.Data.SqlClient.SqlConnection ("server=(local);database=AdventureWorks;Integrated Security=true") 'Dim myConnection3 As New System.Data.SqlClient.SqlConnection ("server=;database=AdventureWorks;Integrated Security=true") Try Dim updateCommand2 As SqlCommand = myConnection2.CreateCommand() myConnection2.Open() DoCommand(updateCommand2, 444, 1.05) 'Dim updateCommand3 As SqlCommand = myConnection3.CreateCommand() 'DoCommand(updateCommand3, 445, 1.04) myTranScope.Complete() Catch ex As Exception ' An error means the transaction can't commit myTranScope.Dispose() Finally myConnection2.Close() 'myConnection3.Close() End Try End Using END CALLOUT C End Sub Sub DoCommand(ByVal updateCommand As SqlCommand, ByVal productID As Integer, ByVal price As Double) ' not efficient but reduces repeated code for this example. updateCommand.CommandText = "usp_Product_UpdatePrice" updateCommand.CommandType = CommandType.StoredProcedure updateCommand.Parameters.Clear() Dim productIDParameter As New SqlParameter("@ProductID", SqlDbType.Int) updateCommand.Parameters.Add(productIDParameter) Dim listPriceParameter As New SqlParameter("@ListPrice", SqlDbType.Money) updateCommand.Parameters.Add(listPriceParameter) productIDParameter.Value = productID listPriceParameter.Value = price updateCommand.ExecuteNonQuery() End Sub End Module