我已经使用SQL Server sqllocaldb创建了本地数据库,并试图在Excel中访问它。运行以下代码会产生以下错误:
打开连接时
“运行时错误'-2147467259(80004005)”:
[DNETLIB [ConnectionOpen(Connect())。] SQL Server都不存在 或访问被拒绝。”
代码如下:
Sub CopyFromDB()
Dim Conn As ADODB.Connection
Dim Data As ADODB.Recordset
Set Conn = New ADODB.Connection
Set Data = New ADODB.Recordset
ConnString = _
"Provider=SQLOLEDB;Integrated Security=SSPI;Persist Security Info=False;Data Source=(LocalDB)\localDB"
Conn.ConnectionString = ConnString
Conn.Open
With Data
.ActiveConnection = Conn
.Source = "tblActor"
.LockType = adLockReadOnly
.CursorType = adOpenDynamic
.Open
End With
Range("A1").CopyFromRecordset Data
Data.Close
Conn.Close
CursorType = 2
End Sub
关于我可能做错了什么的提示?数据源是正确的,并且与授予我在MSSQL Server Management Studio中访问权限的数据相同。