VB.Net by API/System.Data.SqlClient/SqlTransaction

Материал из VB Эксперт
Версия от 12:50, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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.rumandText = "insert into Employee(ID," & _
                      "FirstName,LastName) values(119, " & _
                      ""F","L")"
            mycommand.ExecuteNonQuery()
            mytransaction.Rollback()
            Console.WriteLine("Rollback()")
            mycommand.rumandText = "insert into Employee(ID," & _
                      "FirstName,LastName) values(119, " & _
                      ""F","L")"
            mytransaction.rumit()
        Catch e As Exception
            Console.WriteLine(e.Message)
        Finally
            myconnection.Close()
        End Try
   End Sub
End Class