需求:如果表A 分组是经理或总经理,表A的审核 = '已审核',审核人 = _Name,审核日期 = Date.Today(),表B的当前数量 = 当前数量+表A.数量,否则,提示("你无审核权限!") '否则,提示无审核权限,'回滚事务。
附件中代码报错,如果解决?
下面代码是没有用事务进行审核,可以正常运行,
现想改用事务来实现:如果授权人对表A审核成功,将改变表B的产品的数量,也就是表A审核后的产品的数量加到表B的产品的数量,
如果失败则回到原来,如何实现?
审核事件
If _Group = "经理" Or _Group = "总经理" Then '
Tables("窗口_Table1").Current("审核") = "已审核" '
Tables("窗口_Table1").Current("审核人") = _Name '
Tables("窗口_Table1").Current("审核日期") = Date.Today() '
Tables("窗口_Table1").Current.Save() '保存
Else
MessageBox.show("你无审核权限!")
End If
数据加载
Dim db = HySq-l.DataBaseFactory.CreateDatabase("t04") '
If _Group = "经办" Then
Dim Sq-l = "selec-t * from 表A where 用户ID =" & _用户ID & " ;"
Dim dt As system.data.DataTable = db.ExecuteDataSet(Sq-l).Tables(0)
Dim t As Table = e.Form.Controls("Table1").Table
Else
Dim Sq-l2 = "Selec-t a.*,b.jID " & _
"From " & _
"表A as a " & _
"Inner Join " & _
"gxb as b " & _
"On " & _
"(a.用户ID = b.用户ID) " & _
" Where " & _
"b.jID =" & _用户ID & " ; "
Dim dt2 As system.data.DataTable = db.ExecuteDataSet(Sq-l2).Tables(0)
Dim t2 As Table = e.Form.Controls("Table1").Table
End If
要全部使用sql处理,比如:
Dim db = HySql.DataBaseFactory.CreateDatabase() '获取数据库对象
Dim tran = db.BeginTransaction() '开始一个事务
Dim sql As String = "UPDATE 表A SET 列1 = 'xxx',列2='xxx' WHERE xx列 = 'xxx';"
Try
Dim res As Integer = db.ExecuteNonQuery(tran, sql)
If res = 0 Then
db.RollbackTransaction(tran, True) '回滚事务,同时关闭数据库连接
msgbox("取消事务")
Else
Dim sql2 As String = "UPDATE 表B SET 列1 = 'xxx',列2='xxx' WHERE xx列 = 'xxx';"
res = db.ExecuteNonQuery(tran, sql2)
If res = 0 Then
db.RollbackTransaction(tran, True) '回滚事务,同时关闭数据库连接
msgbox("取消事务")
Else
db.CommitTransaction(tran, True) '提交事务,同时关闭数据库连接
msgbox("执行成功")
End If
End If
Catch ex As Exception
db.RollbackTransaction(tran, True) '回滚事务,同时关闭数据库连接
msgbox("取消事务,原因:" & ex.Message)
End Try
If _Group = "经理" Or _Group = "总经理" Then '
' Tables("窗口_Table1").Current("审核") = "已审核" '
Tables("窗口_Table1").Current("审核人") = _Name '
Tables("窗口_Table1").Current("审核日期") = Date.Today() '
Dim tran = db.BeginTransaction() '开始一个事务
'Dim sq-l As String = "UPDA-TE 表A SET 审核 = '已审核',审核人 = _Name,审核日期 = Date.Today() WHERE _Group = '经理' Or _Group = '总经理';"
Dim sq-l As String = "UPDAT-E 表A SET 审核 = '已审核';"
Try
Dim res As Integer = db.ExecuteNonQuery(tran, sq-l)
If res = 0 Then
db.RollbackTransaction(tran, True) '回滚事务,同时关闭数据库连接
msgbox("取消事务")
Else
Dim sql2 As String = "UPDAT-E 表C SET 当前数量 = 当前数量+ 表B.数量 WHERE 产品编号 = 表B.产品编号;"
res = db.ExecuteNonQuery(tran, sq-l2)
报错:Unknown column '表B.产品编号' in 'where clause',如何处理?
If res = 0 Then
db.RollbackTransaction(tran, True) '回滚事务,同时关闭数据库连接
msgbox("取消事务")
Else
db.CommitTransaction(tran, True) '提交事务,同时关闭数据库连接
msgbox("执行成功")
End If
End If
Catch ex As Exception
db.RollbackTransaction(tran, True) '回滚事务,同时关闭数据库连接
msgbox("取消事务,原因:" & ex.Message)
End Try
Else
MessageBox.show("你无审核权限!") '否则,提示无审核权限
End If