There are many ways to handle transactions, the most common of which is. NET and SQL server transaction processing. ^ _ ^, COM + I don't understand, I heard that it is very strong for transaction processing, but unfortunately
(Have the opportunity to study ).
The following describes SQL server:
Generally, the use of Tranaction is based on a sqlconnection. BeginTransaction is used to start and Commit with Commit, and rollback is used to roll back and forth. The essence of the multiple connections I want to talk about is this. I just used a small trick to catch exceptions using. net. Instead of submitting them one by one, I submitted and rolled back them all in one.
The following is a class that processes multiple transactions. ProcessTransaction. vb
When processing multiple connections, you only need to pass in the corresponding ConnectionString and corresponding SQL statements. Note that ConnectionString is stored in a collection (cCN), while the SQL statement is placed in a string array of the type commandText (), pay attention to putting the SQL statements in one Transaction in it!
Code:
Imports System. Data
Imports System. Data. SqlClient
Public Class ProcessTransactionClass ProcessTransaction
Private c As New Collection
Public Sub ptExecuteNonQuery () Sub ptExecuteNonQuery (ByVal commandText () As String, ByVal cCN As Collection)
Dim s As String
Dim flag As Boolean
For Each s In cCN
If Not ptExecuteNonQuery_test (commandText, s) Then
Flag = False
Exit
Else
Flag = True
End If
Next
Dim sqlTa As SqlTransaction
If flag Then
For Each sqlTa In c
SqlTa. Commit ()
Next
Else
For Each sqlTa In c
SqlTa. Rollback ()
Next
End If
End Sub
Private Function ptExecuteNonQuery_test () Function ptExecuteNonQuery_test (ByVal commandText () As String, ByVal cn As String) As Boolean
Dim sqlcn As New SqlConnection (cn)
Dim sqlCmd As New SqlCommand
Try
Sqlcn. Open ()
Dim sqlTa As SqlTransaction = sqlcn. BeginTransaction ()
C. Add (sqlTa)
SqlCmd. CommandType = CommandType. Text
SqlCmd. Connection = sqlcn
SqlCmd. Transaction = sqlTa
Dim CommStr As String
For Each CommStr In commandText
SqlCmd. CommandText = CommStr
SqlCmd. ExecuteNonQuery ()
Next
Return True
Catch ex As Exception
Return False
Finally
SqlCmd. Dispose ()
Sqlcn. Close ()
Sqlcn. Dispose ()
End Try
End Function
End Class