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

转载于:https://www.cnblogs.com/wgscd/p/5404622.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值