例﹕在ADO执行一个更新语句﹐将B表的记录插入到A表中﹐
cn.execute “insert into A select * from A” 如何得知这条语句执行结束时的状态呢?如下代码﹕
Dim Cn as new adodb.connection
Dim tCount as Long
Dim bCount as LongCn.Open Connstr
Cn.BeginTrans '/开始交易
Cn.Execute "insert into A select * from B"
If Err.Number <> 0 Then
Cn.RollbackTrans '交易失败
MsgBox Err.Description & vbCr
& "备份失败﹗", vbExclamation + vbOKOnly, "温馨提示﹗"
Exit Sub
Else
If Cn.Errors.Count = 0 Then
‘通过函数取出A表 和B表的记录数
bCount = GetRecord("select count(*) as bcount from A", "bcount")
tCount = GetRecord ("select count(*) as tcount from B", "tcount")
‘判断A表和B表的记录数是否相等
If bCount = tCount Then
Cn.CommitTrans '/交易成功
MsgBox "备份成功﹗", vbExclamation + vbOKOnly, "温馨提示﹗"
Exit Sub
Else
Cn.RollbackTrans '/交易失败
MsgBox "备份失败﹗", vbExclamation + vbOKOnly, "温馨提示﹗"
Exit Sub
End if
Else
Cn.RollbackTrans '/交易失败
MsgBox "取消备份﹗", vbExclamation + vbOKOnly, "温馨提示﹗"
Exit Sub
End If
以上程序的问题是﹕
Cn.Execute "insert into A select * from B" 这个语句还未执行完毕时﹐bCount和tCount
的值已取出﹐同时进行判断﹐所以无论怎样交易都是失败的。
只能在Cn.Execute "insert into A select * from B" 语句后取出执行完毕时的状态﹐方可
解决这些问题﹐但不知道这个状态如何取得到 ?
cn.execute “insert into A select * from A” 如何得知这条语句执行结束时的状态呢?如下代码﹕
Dim Cn as new adodb.connection
Dim tCount as Long
Dim bCount as LongCn.Open Connstr
Cn.BeginTrans '/开始交易
Cn.Execute "insert into A select * from B"
If Err.Number <> 0 Then
Cn.RollbackTrans '交易失败
MsgBox Err.Description & vbCr
& "备份失败﹗", vbExclamation + vbOKOnly, "温馨提示﹗"
Exit Sub
Else
If Cn.Errors.Count = 0 Then
‘通过函数取出A表 和B表的记录数
bCount = GetRecord("select count(*) as bcount from A", "bcount")
tCount = GetRecord ("select count(*) as tcount from B", "tcount")
‘判断A表和B表的记录数是否相等
If bCount = tCount Then
Cn.CommitTrans '/交易成功
MsgBox "备份成功﹗", vbExclamation + vbOKOnly, "温馨提示﹗"
Exit Sub
Else
Cn.RollbackTrans '/交易失败
MsgBox "备份失败﹗", vbExclamation + vbOKOnly, "温馨提示﹗"
Exit Sub
End if
Else
Cn.RollbackTrans '/交易失败
MsgBox "取消备份﹗", vbExclamation + vbOKOnly, "温馨提示﹗"
Exit Sub
End If
以上程序的问题是﹕
Cn.Execute "insert into A select * from B" 这个语句还未执行完毕时﹐bCount和tCount
的值已取出﹐同时进行判断﹐所以无论怎样交易都是失败的。
只能在Cn.Execute "insert into A select * from B" 语句后取出执行完毕时的状态﹐方可
解决这些问题﹐但不知道这个状态如何取得到 ?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货