简单代码
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
同时保存几个表的数据,如果一个表的保存出错,做出事务回滚动作,即可以保证的程序的容错性很好。
注意的地方:事务与连接是整对触发与对应的,不管在查询,修改,删除的过程中。其他的事务不能与非本方的连接配对