VB6 Access 事务
On Error GoTo err_trans
intTrans = conn.BeginTrans '开始事务
X = count
For i = 0 To X
sql="insert into Sales(dish_id,uid) values......."
conn.Execute sql
Next i
conn.CommitTrans '提交事务
intTrans = 0
MsgBox "OK"
exit_trans:
Exit Function'退出过程
err_trans: '错误跳到这儿处理
'如果是事务处理中出错,则事务回滚
If intTrans = 1 Then
conn.RollbackTrans
MsgBox "false"
End If
GoTo exit_trans '跳转到exit_trans
注意:多条语句合并执行会导致无法准确返回执行结果
如:
For i = 0 To X
sql= sql & "insert into Sales(dish_id,uid) values......."
Next i
conn.Execute sql
conn.CommitTrans '提交事务
DAO 模式,Library DAO
C:\Program Files\Common Files\dao\dao360.dll
Microsoft DAO 3.6 Object Library
Sub testTrans()
On Error GoTo trans_err
Dim sql As String
Dim gdbCurrentDB As Database
Set gdbCurrentDB = DBEngine.OpenDatabase("C:\wgscd\Db\test.mdb")
BeginTrans
sql = "UPDATE users SET age=228 WHERE id=1"
gdbCurrentDB.Execute sql
sql = "insert into users (name,sex,a5ge) values('wgscd3','m',43)"
gdbCurrentDB.Execute sql
CommitTrans
gdbCurrentDB.Close
Set gdbCurrentDB = Nothing
MsgBox "OK"
Exit Sub
trans_err:
' Rollback
gdbCurrentDB.Close
Set gdbCurrentDB = Nothing
MsgBox "false"
End Sub