sql-server - Access VBA 连接以测试 SQL Server 是否存在

标签 sql-server ms-access vba

我有一个 Access 应用程序,需要连接到几个可能的 SQL Server 之一(即连接链接表),并且我有这些可能的 SQL Server 实例名称的列表。当应用程序启动时,它需要查看哪些可能的服务器可用。考虑到使用 SQLBrowseConnect 或 NetServerEnum 等解决方案的缓慢性,我想知道是否有一种干净、快速的方法可以根据 SQL Server 的名称“ping”它。

最佳答案

我们使用传递查询VerifyConnection,它只打开一个小表。

测试更改连接并检查它是否可以读取表:

Public Function IsSqlServer( _
    ByVal TestNewConnection As Boolean, _
    Optional ByVal Hostname As String, _
    Optional ByVal Database As String, _
    Optional ByVal Username As String, _
    Optional ByVal Password As String, _
    Optional ByRef ErrNumber As Long) _
    As Boolean

    Const cstrQuery     As String = "VerifyConnection"

    Dim dbs             As DAO.Database
    Dim qdp             As DAO.QueryDef
    Dim rst             As DAO.Recordset

    Dim booConnected    As Boolean
    Dim strConnect      As String
    Dim strConnectOld   As String
    Dim booCheck        As Boolean

    Set dbs = CurrentDb
    Set qdp = dbs.QueryDefs(cstrQuery)

    If Hostname & Database & Username & Password = "" Then
        If TestNewConnection = False Then
            ' Verify current connection.
            booCheck = True
        Else
            ' Fail. No check needed.
            ' A new connection cannot be checked with empty parameters.
        End If
    Else
        strConnectOld = qdp.Connect
        strConnect = ConnectionString(Hostname, Database, Username, Password)
        If strConnect <> strConnectOld Then
            If TestNewConnection = False Then
                ' Fail. No check needed.
                ' Tables are currently connected to another database.
            Else
                ' Check a new connection.
                qdp.Connect = strConnect
                booCheck = True
            End If
        Else
            ' Check the current connection.
            strConnectOld = ""
            booCheck = True
        End If
    End If

    On Error GoTo Err_IsSqlServer

    ' Perform check of a new connection or verify the current connection.
    If booCheck = True Then
        Set rst = qdp.OpenRecordset()
        ' Tried to connect ...
        If ErrNumber = 0 Then
            If Not (rst.EOF Or rst.BOF) Then
                ' Success.
                booConnected = True
            End If
            rst.Close
        End If

        If strConnectOld <> "" Then
            ' Restore old connection parameters.
            qdp.Connect = strConnectOld
        End If
    End If

    Set rst = Nothing
    Set qdp = Nothing
    Set dbs = Nothing

    IsSqlServer = booConnected

Exit_IsSqlServer:
    Exit Function

Err_IsSqlServer:
    ' Return error.
    ErrNumber = Err.Number
    ErrorMox "Tilslutning af database"
    ' Resume to be able to restore qdp.Connect to strConnectOld.
    Resume Next

End Function

这样您就可以检查到单个表的完整路线。

关于sql-server - Access VBA 连接以测试 SQL Server 是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37426141/

相关文章:

java sql异常from子句中的语法错误

vba - 比较 VBA 中的日期

excel - 使用 filesystemobject 检查文件是否存在

sql-server - SQL Server 存储过程中的表变量插入性能不佳

sql - 将时间 23 :59:59. 999 添加到结束日期之间

sql - 获取日期 SQL Server 的最大可能时间

sql-server - 使用 Azure SQL 数据库后端在 MS Access 中添加和编辑表的工作流

SQL Server 在脚本结束前强制将 Select 语句的结果输​​出到输出窗口

mysql - 将Access数据库与Mysql服务器链接并使用Access作为前端[表问题]

excel - 如何使 msgbox 成为变量并在错误处理程序上触发它?