弹来弹去跑马灯!

VB6 Access 事务(Transaction)

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 '提交事务

参考:http://wenku.baidu.com/link?url=xTP1FCbU5vC6iYD1Ud6RVzSiBllB_fgr4bJHLtXUVTGMAMmaSnLxI9Nfy-44wS6roLxjqSp_TainoHdHa0L5ue6-003V3mfWHqc8-KQ55Va

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

posted @ 2016-04-18 15:22  wgscd  阅读(1145)  评论(0编辑  收藏  举报