SqlTransaction.BeginTransaction() : SqlTransaction « System.Data.SqlClient « VB.Net by API






SqlTransaction.BeginTransaction()

  
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()

        mytransaction = myconnection.BeginTransaction()

        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