Simple Transaction Commit and RollBack: SqlTransaction has completed; it is no longer usable.
Imports System
Imports System.Data
Imports System.Data.SqlClient
public class MainClass
Shared Sub Main()
Dim myconnection As SqlConnection
Dim mycommand As SqlCommand
Dim mytransaction As SqlTransaction
Dim ConnectionString As String
myconnection = New SqlConnection("server=(local)\SQLEXPRESS;" & _
"integrated security=sspi;database=MyDatabase")
myconnection.Open()
'start a transaction
mytransaction = myconnection.BeginTransaction()
'configure command object to use transaction
mycommand = New SqlCommand
mycommand.Connection = myconnection
mycommand.Transaction = mytransaction
Try
mycommand.CommandText = "insert into Employee(ID," & _
"FirstName,LastName) values(119, " & _
"'F','L')"
mycommand.ExecuteNonQuery()
mytransaction.Rollback()
Console.WriteLine("Rollback()")
mycommand.CommandText = "insert into Employee(ID," & _
"FirstName,LastName) values(119, " & _
"'F','L')"
mytransaction.Commit()
Catch e As Exception
Console.WriteLine(e.Message)
Finally
myconnection.Close()
End Try
End Sub
End Class
Related examples in the same category