戏耍Transaction,多个连接的Transaction处理(非COM+)

处理事务的方法很多,其中最常用的是.NET和sql server的事务处理,^_^,COM+俺不懂,听说用于事务处理很强,可惜了
(有机会 定要研究研究)。

下面的说明是SQL server:
通常的Tranaction的使用是基于一个sqlconnection的,使用BeginTransaction来开始和用Commit提交,rollback来回滚。我要说的多个连接的实质还是这样,只是自己使用了一个小伎俩,利用.net的捕捉异常的机制,不再一一提交,而是统一提交和回滚。

下面是处理多个Transaction的一个类。ProcessTransaction.vb
处理多个连接时只要把相应的ConnectionString和相应的Sql语句传入就搞定了。注意的时,ConnectionString存放在一个collection(cCN)中,Sql语句放在一个类型时string的数组中commandText(),注意把在一个Transaction的sql语句统一放在里面啊!

Code:

None.gif Imports  System.Data
None.gif
Imports  System.Data.SqlClient
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Public   Class ProcessTransaction Class ProcessTransaction
InBlock.gif
InBlock.gif
InBlock.gif    
Private c As New Collection
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub ptExecuteNonQuery()Sub ptExecuteNonQuery(ByVal commandText() As StringByVal cCN As Collection)
InBlock.gif
InBlock.gif        
Dim s As String
InBlock.gif
InBlock.gif        
Dim flag As Boolean
InBlock.gif        
For Each s In cCN
InBlock.gif            
If Not ptExecuteNonQuery_test(commandText, s) Then
InBlock.gif                flag 
= False
InBlock.gif                
Exit For
InBlock.gif            
Else
InBlock.gif                flag 
= True
InBlock.gif            
End If
InBlock.gif        
Next
InBlock.gif
InBlock.gif        
Dim sqlTa As SqlTransaction
InBlock.gif        
If flag Then
InBlock.gif            
For Each sqlTa In c
InBlock.gif                sqlTa.Commit()
InBlock.gif            
Next
InBlock.gif        
Else
InBlock.gif            
For Each sqlTa In c
InBlock.gif                sqlTa.Rollback()
InBlock.gif            
Next
InBlock.gif        
End If
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Function ptExecuteNonQuery_test()Function ptExecuteNonQuery_test(ByVal commandText() As StringByVal cn As StringAs Boolean
InBlock.gif
InBlock.gif        
Dim sqlcn As New SqlConnection(cn)
InBlock.gif        
Dim sqlCmd As New SqlCommand
InBlock.gif        
Try
InBlock.gif            sqlcn.Open()
InBlock.gif            
Dim sqlTa As SqlTransaction = sqlcn.BeginTransaction()
InBlock.gif            c.Add(sqlTa)
InBlock.gif
InBlock.gif            sqlCmd.CommandType 
= CommandType.Text
InBlock.gif            sqlCmd.Connection 
= sqlcn
InBlock.gif            sqlCmd.Transaction 
= sqlTa
InBlock.gif            
Dim CommStr As String
InBlock.gif            
For Each CommStr In commandText
InBlock.gif                sqlCmd.CommandText 
= CommStr
InBlock.gif                sqlCmd.ExecuteNonQuery()
InBlock.gif            
Next
InBlock.gif            
Return True
InBlock.gif        
Catch ex As Exception
InBlock.gif            
Return False
InBlock.gif        
Finally
InBlock.gif            sqlCmd.Dispose()
InBlock.gif            sqlcn.Close()
InBlock.gif            sqlcn.Dispose()
InBlock.gif        
End Try
InBlock.gif
ExpandedSubBlockEnd.gif    
End Function

ExpandedBlockEnd.gif
End Class

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值