begin transac
begin try
--work space
--end work space
commit
end try
begin catch
rollback
end catch
try write pattern this again krub
Date :
2019-04-04 09:34:44
By :
adminliver
No. 3
Guest
Transactions ข้ามดาเบส
Code (VB.NET)
Imports System.Data
Imports System.Data.SqlClient
Imports MySql.Data
Public Class _Sample
Public Shared Async Sub Example1() 'As Task(Of String)
Dim objUser As New Users With {
.UserID = 1,
.UserName = "nongporn"
}
Using conn As IDbConnection = New SqlClient.SqlConnection("SQL Server Connection String")
conn.Open()
Using trans = conn.BeginTransaction()
Try
Dim row1 As Integer = Await conn.Insert("Users", objUser, trans)
'***** Business so on. *****
If row1 > 0 Then
objUser.UserID = 2
objUser.UserName = "Jimi"
Dim row2 As Integer = Await conn.Insert("Users", objUser, trans)
End If
trans.Commit()
Catch ex As Exception
trans.Rollback()
End Try
End Using
End Using
Using conn As IDbConnection = New MySqlClient.MySqlConnection("MySQL/Maria server Connection String")
conn.Open()
Using trans = conn.BeginTransaction()
Try
Dim row1 As Integer = Await conn.Insert("Users", objUser, trans)
If row1 > 0 Then
objUser.UserID = 2
objUser.UserName = "Jimi"
Dim row2 As Integer = Await conn.Insert("Users", objUser, trans)
End If
trans.Commit()
Catch ex As Exception
trans.Rollback()
End Try
End Using
End Using
End Sub
''' <summary>
''' Table dbo.Users
''' </summary>
Public Class Users
Public Property UserID As Integer 'จำเป็น
Public Property UserName As String 'จำเป็น
Public Property Password As String
Public Property Tel As String
'So on.
End Class
End Class