[英]Method open of object _connection failed
I am trying to connect to an SQL ce database within Excel. 我正在尝试连接到Excel中的SQL ce数据库。 It keeps giving me the:
它一直给我:
Method Open of object _connection failed
Error message whenever it gets to Conn.Open
. 每当到达
Conn.Open
时出现错误消息。
I've never used ADO before so I'm not sure what's going wrong. 我以前从未使用过ADO,所以不知道出了什么问题。
Below is my connection script. 下面是我的连接脚本。
Sub SQLCeConnect()
Dim Conn As New ADODB.Connection
Dim Query As New ADODB.Command
Dim ConnStr As String
Dim RecordSet As New ADODB.RecordSet
Dim i As Integer
'the connection string
ConnStr = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=X:\ADOTEST\MYDB.sdf;"
'Open ADO Connection
Conn.ConnectionString = ConnStr
Conn.Open
Query.ActiveConnection = Conn
Query.CommandText = "SELECT * From DoorLayers"
Set RecordSet = Query.Execute
RecordSet.Close
Conn.Close
Conn.ConnectionString = ""
Do While Not RecordSet.EOF
For i = 0 To RecordSet.Fields.Count - 1
Debug.Print RecordSet.Fields(i).Name, RecordSet.Fields(i).Value
Next
RecordSet.MoveNext
Loop
RecordSet.Close
End Sub
The is usually a bad connection string, but yours looks fine. 通常是一个错误的连接字符串,但您看起来不错。 The next most common cause is that you don't have the OLEDB provider drivers on your desktop.
下一个最常见的原因是您的桌面上没有OLEDB提供程序驱动程序。 You can download it here
您可以在这里下载
http://www.microsoft.com/en-us/download/details.aspx?id=5821 http://www.microsoft.com/en-us/download/details.aspx?id=5821
Are you closing the connection before use it on WHILE . 在WHILE上使用连接之前,是否要关闭连接? You must close it after use the while instruction.
您必须在使用while指令后将其关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.