SQL = "select * from [Table] WITH (ROWLOCK) where ProNum2='001' "
rs.Open SQL, cnMain, 1, 3
rs("cun") = 1000
rs.Update
rs.Close
Set rs = Nothing
cnMain.Execute (" update [Table1] WITH (ROWLOCK) set pronum2=1' where pronum='0001' ")
cnMain.Execute (" update [Table2] WITH (ROWLOCK) set pronum2='2' where pronum='0002' ")
rs.Open SQL, cnMain, 1, 3
rs("cun") = 1000
rs.Update
rs.Close
Set rs = Nothing
cnMain.Execute (" update [Table1] WITH (ROWLOCK) set pronum2=1' where pronum='0001' ")
cnMain.Execute (" update [Table2] WITH (ROWLOCK) set pronum2='2' where pronum='0002' ")
conn.execute("insert into test(aa) values('"&sex&"')")
if Err <> 0 then
conn.RollbackTrans '回滚事务else
conn.CommitTrans '提交事务22
end if
而不是conn
其实我只想解决这样的问题:
不想让多个人同时修改同一条数据的时候出现数据乱串的现象cnMain.BeginTrans '事务开始 SQL = "select * from [Table] WITH (ROWLOCK) where ProNum2='001' "
rs.Open SQL, cnMain, 1, 3
rs("cun") = 1000
rs.Update
rs.Close
Set rs = Nothing
cnMain.Execute (" update [Table1] set pronum2=1' where pronum='0001' ")
cnMain.Execute (" update [Table2] set pronum2='2'where pronum='0002' ")
If Err <> 0 Then
cnMain.RollbackTrans '回滚事务 Else
cnMain.CommitTrans '提交事务End If
conn.BeginTrans '事务开始
On Error Resume Next '增加的代码
strSql1 = "update [password] set [name]='aaa' where id=14" '第一个sql语句为update,语法正确
strSql2 = "insert into a(num) values('a')" '错误的sql语句
strSql3 = "update [password] set [name]='bbb' where id=15" '正确的sql语句
Call conn.Execute(strSql1)
Call conn.Execute(strSql2)
Call conn.Execute(strSql3)
If Err.Number = 0 Then
conn.CommitTrans '如果没有conn错误,则执行事务提交
Else
conn.RollbackTrans '否则回滚
'回滚后的其他操作
MsgBox ("数据库错误!错误日志:" & Err.Description)
Exit Sub
End If