You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA连接SQL数据库的Connection String无法正常使用求助

VBA连接SQL数据库获取表名报错求助

问题背景

我需要通过VBA连接SQL Server数据库,执行查询获取Asset_Output库中所有表名及修改时间,但连接字符串配置后出现报错。

第一次报错情况

使用以下连接字符串:

cn.Open "Provider=SQLNCLI11;Integrated Security=SSPI;Initial Catalog=Asset_Output;Data Source=GA-SQL"

运行后报错:

无法找到数据源名称并且未指定默认驱动程序

修改后的报错情况

将连接字符串中的Integrated Security=SSPI改为Integrated Security=true后,出现新错误:

[DBNETLIB][ConnectionOpen (Connect()).]SQL Server 不存在或访问被拒绝

完整VBA代码

Dim cn As ADODB.Connection
Dim SQLStr As String
Dim rs As ADODB.Recordset
Dim iCols As Integer
Set rs = New ADODB.Recordset


SQLStr = " Use Asset_Output  " & _
         " select name, modify_date from sys.tables order by  modify_date desc"

Set cn = New ADODB.Connection
cn.Open "Provider=SQLNCLI11;Integrated Security=true;Initial Catalog=Asset_Output;Data Source=GA-SQL"

rs.Open SQLStr, cn

With Worksheets("Input").Range("E2:Z50000")
    .ClearContents
    Worksheets("Input").Range("E2").CopyFromRecordset rs
End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

内容的提问来源于stack exchange,提问作者Mikku

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 21:28:22