Play Transaction, Transaction processing for multiple connections (non-COM +)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.