【问题标题】:VBA connect with ADO to SQL Server (Windows authenticated)VBA 与 ADO 连接到 SQL Server(Windows 认证)
【发布时间】:2021-06-16 16:03:33
【问题描述】:

我在尝试通过 VBA 连接到使用 Windows 身份验证的 SQL Server 时收到运行时错误 3709。

问题出现在这一行:

.ActiveConnection = conn

这是用于连接的完整代码:

Dim strConn As String
Dim wsReport As Worksheet
Dim col As Integer

strConn = "Provider=SQLOLEDB;"
strConn = strConn & "Server=" & Server_Name & ";"
strConn = strConn & "Database=" & Database_Name & ";"
strConn = strConn & "Trusted_Connection=yes;"
strConn = strConn & "Integrated Security=True;"

Set conn = New ADODB.Connection

With conn
    .ConnectionString = strConn
    .CursorLocation = adUseClient
End With

Set rst = New ADODB.Recordset

With rst

    .ActiveConnection = conn
    .Open Source:=SQL_Statement
End With

Set wsReport = ThisWorkbook.Worksheets.Add

With wsReport
    For col = 0 To rst.Fields.Count - 1
        .Cells(1, col + 1) = rst.Fields(col).Name
    
    Next col
End With

或者ADO现在已经过时了?

【问题讨论】:

  • 错误描述是什么?
  • 尝试删除“Trusted_Connection=yes”。单独的“Integrated Security=True”将提供 Windows 身份验证。 ADO 是一项“成熟”的技术,15 年来没有得到改进,但并未正式弃用。不过,Windows 附带的 SQLOLEDB 驱动程序已弃用,因此您应该转而使用 MSOLEDBSQL,单独下载并安装。
  • 错误消息是:“错误消息:”运行时错误'3709'连接不能用于执行此操作。在这种情况下,它要么是关闭的,要么是无效的。”
  • 我尝试将提供程序更改为 MSOLEDBSQL,并删除“Trusted_Connection=yes”我仍然有同样的问题。我还尝试使用“cn.Open strConn”更改代码,就像在链接中一样。然后我得到一个不同的运行时错误“运行时错误'-21472127887(80040e21)'”

标签: sql sql-server windows-authentication ado


【解决方案1】:

解决方案是“Provider=MSOLEDBSQL;Server=XX;Database=XXXX;Trusted_Connection=yes;”

这个网站根据具体情况给出了所有不同的组合,展示了与 SQL-Server 连接的复杂性:

https://www.connectionstrings.com/ole-db-driver-for-sql-server/

【讨论】:

    猜你喜欢
    • 2014-12-24
    • 2012-04-25
    • 2022-01-06
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    相关资源
    最近更新 更多