关于连接与事务的处理

简单代码 

    Private Sub ExcuteSaveCommand()
            Dim blnCommit As Boolean = True
            Dim objConn As SqlClient.SqlConnection = DAL.clsDBHandler.CreateDBConnection
            Dim objTran As SqlClient.SqlTransaction = objConn.BeginTransaction
            If CurrentItem.SysID = -1 Then
                fillGenerateServerSideObjData(CurrentItem.MyBaseObjProp, EnObjInsertOrUpdate.EnInsert)
                Dim intR As Integer
                intR = BLL.BL_clstblItemMaster.InsertData(CurrentItem.MyBaseObjProp, objConn, objTran)
                If intR < 0 Then
                    blnCommit = False
                    objTran.Rollback()
                    Exit Sub
                End If
                If obsLanguage.Count > 0 Then
                    For i As Integer = 0 To obsLanguage.Count - 1
                        Dim obj As New MODEL.clstblItemDescription
                        obj.PLU = CurrentItem.PLU
                        obj.LanguageID = obsLanguage(i).LanguageID
                        obj.ItemName = obsLanguage(i).ItemName
                        obj.MarkDel = 0
                        fillGenerateServerSideObjData(obj, EnObjInsertOrUpdate.EnInsert)
                        Dim intR1 As Integer
                        intR1 = BLL.BL_clstblItemDescription.InsertData(obj, objConn, objTran)
                        If intR1 < 0 Then
                            blnCommit = False
                            objTran.Rollback()
                            Exit Sub
                        End If
                    Next
                End If
                If IsNothing(CurrentItem) = False Then
                    objDetailPrice.Style = CurrentItem.Style
                    fillGenerateServerSideObjData(objDetailPrice, EnObjInsertOrUpdate.EnInsert)
                    Dim intR2 As Integer
                    intR2 = BLL.BL_clstblItemRetailPrice.InsertData(objDetailPrice)
                    If intR2 < 0 Then
                        blnCommit = False
                        objTran.Rollback()
                        Exit Sub
                    End If
                End If
            Else

                fillGenerateServerSideObjData(CurrentItem.MyBaseObjProp, EnObjInsertOrUpdate.EnUpdate)
                BLL.BL_clstblItemMaster.UpdateData(CurrentItem.MyBaseObjProp)

                For i As Integer = 0 To obsLanguage.Count - 1
                    Dim list As New List(Of MODEL.clstblItemDescription)
                    list = BLL.BL_clstblItemDescription.FillData(objConn, objTran, , CurrentItem.PLU, obsLanguage(i).LanguageID)
                    If list.Count = 0 Then
                        Dim obj As New MODEL.clstblItemDescription
                        obj.PLU = CurrentItem.PLU
                        obj.LanguageID = obsLanguage(i).LanguageID
                        obj.ItemName = obsLanguage(i).ItemName
                        obj.MarkDel = 0
                        fillGenerateServerSideObjData(obj, EnObjInsertOrUpdate.EnInsert)
                        Dim intR3 As Integer
                        intR3 = BLL.BL_clstblItemDescription.InsertData(obj, objConn, objTran)
                        If intR3 < 0 Then
                            blnCommit = False
                            objTran.Rollback()
                            Exit Sub
                        End If
                    Else
                        list(0).MarkDel = 0
                        list(0).LanguageID = obsLanguage(i).LanguageID
                        list(0).ItemName = obsLanguage(i).ItemName
                        fillGenerateServerSideObjData(list(0), EnObjInsertOrUpdate.EnUpdate)
                        BLL.BL_clstblItemDescription.UpdateData(list(0))
                    End If
                Next
                fillGenerateServerSideObjData(objDetailPrice, EnObjInsertOrUpdate.EnUpdate)
                BLL.BL_clstblItemRetailPrice.UpdateData(objDetailPrice)
            End If
            If blnCommit = False Then
                objTran.Rollback()
                Exit Sub
            Else
                objTran.Commit()
                MessageBox.Show("Success")
            End If
            '    CurrentItem.SaveCommand.Execute(Nothing)
            '   MessageBox.Show("Success")
        End Sub

同时保存几个表的数据,如果一个表的保存出错,做出事务回滚动作,即可以保证的程序的容错性很好。

注意的地方:事务与连接是整对触发与对应的,不管在查询,修改,删除的过程中。其他的事务不能与非本方的连接配对

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值