如何解决如何检测 ADO 连接错误?
我整理了一个函数(在一个ASP Classic网站的VBScript中)打开一个新的MS SQL ADO连接,发现如果启用错误处理,会出现错误代码424,描述为“Object Required” .我查了一下这个错误,我在上面找到的一切都与 Microsoft Excel 相关。仍会创建有效且有效的连接对象,但您无法在代码中做出决定(使用错误响应),以确定是否创建了正确的连接对象。
我的问题是,这是预期的行为,如果是,我将如何确保连接对象已正确实例化?请注意,当我尝试就此行为与 Microsoft 联系时,页面上出现了以下声明:
使用 vba 标签以及任何其他相关标签将 VBA 编程问题发布到 Stack Overflow。
该函数的代码片段如下:
On Error Resume Next
' Instantiate the new DB Connection object:
Set objTemporary = Server.CreateObject("ADODB.Connection")
objTemporary.ConnectionTimeout = cServerConnectionTimeout
objTemporary.CommandTimeout = cServerCommandTimeout
objTemporary.ConnectionString = sSQLConnectionString
objTemporary.Mode = adModeReadWrite
objTemporary.Open
If Debug_OpenDBConnection = True Then
Response.Write("<br>LINE-336 (dataaccess.asp): This is the value of the objTemporary object: ") & objTemporary & "<br>"
Response.End
End If
' Check to see if an error occurred:
If Err.Number <> 0 Then
If Debug_OpenDBConnection = True Then
Response.Write("<br>LINE-345 (dataaccess.asp): This is the value of Err.Number: ") & Err.Number & "<br>"
Response.Write("This is the value of Err.Source: ") & Err.Source & "<br>"
Response.Write("This is the value of Err.Description: ") & Err.Description & "<br>"
Response.End
Else
Redim aOpen_DB_Connection(4)
aOpen_DB_Connection(0) = False
aOpen_DB_Connection(1) = 2
aOpen_DB_Connection(2) = Err.Number
aOpen_DB_Connection(3) = Err.Source
aOpen_DB_Connection(4) = Err.Description
OpenDBConnection = aOpen_DB_Connection
Exit Function
Response.End
End If
Else ' No error occured,so exit function,returning the new connection object:
Redim aOpen_DB_Connection(1)
aOpen_DB_Connection(0) = True
aOpen_DB_Connection(1) = objTemporary
OpenDBConnection = aOpen_DB_Connection
Exit Function
Response.End
End If
如有必要,可以包含更多函数代码,包括 sSQLConnectionString
变量的内容。有趣的是,如果在第一个对象打开之后和关闭之前的任何时候确实随后发出第二个 OPEN 命令,错误报告对象将正常工作(报告正确的错误类型)。这似乎是因为该对象可供错误报告机制发挥作用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。