如何最好地在 Access 或 Excel 中使用 VBA 来测试 ODBC 连接?
给定一个预先配置的 ODBC 系统 DSN,我想编写一个函数,使用 VBA 优雅地测试该连接。
Private Function TestConnection(ByVal dsnName As String) As Boolean
' What goes here?? '
End Function
编辑:澄清一下,系统 DSN 指向外部 SQL Server 2005 数据库,并使用 Windows NT 身份验证。
我尝试过的一种方法是向目标数据库发送一些随机查询并捕获错误。 如果查询有效,则返回 true。 如果有错误则返回 false。 这工作得很好,但感觉……很笨拙。 是否有一种更优雅的方法,尤其是不依赖于 On Error Goto 的方法?
注意:我正在处理的是旧版 Access 2000 数据库,因此任何解决方案都不能具有任何 Access 2007 或 2003 依赖项。 我想让它对 VBA 通用,但如果 Access 中有一种简单的方法也很好。
非常感谢任何建议。
Given a pre-configured ODBC System DSN, I'd like to write a function that gracefully tests that connection using VBA.
Private Function TestConnection(ByVal dsnName As String) As Boolean
' What goes here?? '
End Function
Edit: To clarify, the System DSNs are pointing to external SQL Server 2005 databases, with Windows NT authentication.
One approach I've tried is to send some random query to the target database and catch the error. If the query works, return true. If there's an error then return false. This works just fine but it feels...kludgy. Is there a more elegant way, especially one that doesn't rely on On Error Goto ?
Note: It's a legacy Access 2000 database I'm working on, so any solution can't have any Access 2007 or 2003 dependencies. I'd like to make it generic to VBA, but if there's a simple way in Access that's fine too.
Much obliged for any advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:DSN 格式可以是“DSN=MyDSN;UID=myuser;PWD=myPwd;”
查看这里以获取连接字符串
EDIT: DSN Format could be "DSN=MyDSN;UID=myuser;PWD=myPwd;"
Look this for connection strings
我来不及为您的问题提供有用的答案,但我来到这里是因为我想看看 StaCkOverflow 是否有比我当前用于测试 ADODB 连接的代码更好的答案。
...事实证明答案是“否”,所以我将发布代码以供参考:其他人会发现它有用。
编码说明:这不是一个通用答案:它是封装 ADODB.Connection 对象的类中的方法,并且假定对象“m_objConnect”存在。
TestConnection:用于发布 ADODB.Connection 对象调试信息的 VBA 类方法
这会打印出连接字符串、当前状态、ADODB 错误列表(如果有)以及连接的完整列表命名属性。
I'm too late to give you a useful answer to your question, but I came here because I wanted to see if StaCkOverflow has a better answer than the code I'm currently using to test ADODB connections.
...It turns out that the answer is 'No', so I'll post the code for reference: someone else will find it useful.
Coding notes: this isn't a generic answer: it's a method from a class encapsulating the ADODB.Connection object, and it assumes the existence of object 'm_objConnect'.
TestConnection: a VBA Class method for publishing debugging information for an ADODB.Connection object
This prints out the connection string, the current status, a list of ADODB errors (if any) and a full listing of the onnection's named properties.
Share and enjoy: and watch out for line-breaks, helpfully inserted where they will break the code by your browser (or by StackOverflow's formatting functions).
没有神奇的功能可以在不实际连接和尝试操作的情况下测试这一点。
如果您对随机查询部分感觉不好 - 您可以查询系统表
For Access
For SQL Server
There no magic function that will test this without actually connecting and trying an operation.
If you feel bad about the random query part - you can query the system tables
For Access
For SQL Server
如果您只需测试数据库服务器是否实际可用,则可以这样做,尽管这里说它不能。
在这种情况下,您可以尝试打开到特定服务器和端口的 TCP 连接。
例如,SQL Server 的默认实例侦听 TCP 端口 1433。在 VBA 中尝试简单的 TCP 连接会告诉您是否成功。 仅当成功时我才会使用 ODBC 连接进行查询。
这更加优雅和高效。 它将消除 ODBC 测试代码中的任何“严重”错误。 但是,正如我所说,它仅适用于您需要测试数据库服务器实例是否存在/可用性的情况。
If you merely have to test that the database server is actually available, this can be done, despite what is being said here that it cannot.
In that case, you can attempt to open a TCP connection to the specific server and port.
The default instance of SQL Server, for example, listens on TCP port 1433. Attempting a simple TCP connection in VBA will tell you if it succeeds or not. Only if that is successful I would query using the ODBC connection.
This is a lot more graceful and efficient. It would remove any "gross" error from your ODBC test code. However, as I said, it is only applicable if you need to test for the mere existence/availability of the database server instance.