首页 > 解决方案 > 使用 SSL 证书连接 SQL 服务器的 VBA 连接字符串

问题描述

我在我的 VB 脚本中使用 VBA 连接字符串连接 SQL 服务器。它之前运行顺利。

最近服务器团队安装了 SSL 证书,其中我的宏现在没有运行,并说明 SSL 安全错误,如下图所示。

错误

谁能帮我解决这个问题。

提前致谢!!!

问候,

标签: vbassl-certificate

解决方案


我会在打开连接字符串之前连接它所需的值,而不是 on cnSrc.Open

我使用 VBA 并使用 ADO 每天查询 SQL 服务器,我对连接字符串使用以下格式:

Provider=SQLOLEDB.1;User ID=UserName;Password=Password;Data Source=SeverName;Initial Catalog=DataBaseName

您可以使用下面的代码进行测试:

Private Sub TestConnection()

    Dim cnSrc As ADODB.Connection
    Dim CONNECTION_STRING As String
    Dim cnServer As String, cndb As String, cnUser As String, cnPwd As String

    cnServer = "SeverName"
    cndb = "DataBaseName"
    cnUser = "UserName"
    cnPwd = "Password"

    CONNECTION_STRING = "Provider=SQLOLEDB.1;User ID=" & cnUser & ";Password=" & cnPwd & ";" & _
                        "Data Source=" & cnServer & ";Initial Catalog=" & cndb

    On Error GoTo CleanFail
    Set cnSrc = New ADODB.Connection
        cnSrc.Open CONNECTION_STRING

    'bit wise comparison to check the connection's state
    Debug.Assert (cnSrc.State And adStateOpen) = adStateOpen    'Debug.Assert pauses execution if false


CleanExit:
    'close the connection
    If Not cnSrc Is Nothing Then: If (cnSrc.State And adStateOpen) = adStateOpen Then cnSrc.Close
    Set cnSrc = Nothing
    Exit Sub

CleanFail:
    Resume CleanExit

End Sub

推荐阅读