【问题标题】:What error mode to use when running an ADO transaction in classic ASP?在经典 ASP 中运行 ADO 事务时使用什么错误模式?
【发布时间】:2012-03-26 01:18:19
【问题描述】:
我在经典的 ASP 页面中使用 ADO 和 VBScript 运行数据库事务,这涉及对连接对象的 Execute 方法的多次调用(即 conn.Execute)。
我发现我需要在页面顶部设置“On Error Resume Next”,以便当任何事务调用(即 conn.Execute )失败时,我可以使用回滚代码跟踪它。
即使在我的经典 ASP 页面中错误模式是“On Error GoTo 0”而不是“On Error Resume Next”,我是否可以运行 ADO 事务? 回滚事务的示例代码如下。
'Rollback transaction if a previous conn.Execute fails
if err.Number <> 0 then
if tranCount = 1 then
conn.RollbackTran
tranCount = 0
end if
end if
【问题讨论】:
标签:
asp-classic
transactions
vbscript
ado
【解决方案1】:
有趣的问题!
我通常不从经典 ASP 进行数据库事务,所以我不确定 conn.Errors 是否会在 ASP 脚本之前捕获错误。不过你至少可以试试……
IF conn.Errors.Count > 0 THEN
response.write "whoops"
END IF
否则它可能会帮助您了解 On Error Resume Next 仅在当前范围内有效。 (见下面的代码)
<%
response.write "start"
BadFunction()
response.write "middle" '//<--- This will be printed
dim b : b = 8 / 0 '//Division by zero
response.write "end" '//<-- This will NOT be printed!
Function BadFunction()
On Error Resume Next
dim a : a = 9 / 0 '//Division by zero
BadFunction = a
End function
%>