【问题标题】:OleDB Data provider can not be found VBA/Excel找不到 VBA/Excel 的 OleDB 数据提供程序
【发布时间】:2013-02-12 15:46:36
【问题描述】:

我对 VBA 几乎不熟悉(在学校有过一些课程,仅此而已)。现在我需要从 Excel 文件连接到 Oracle 数据库(在远程服务器上运行)。我环顾四周,找到了一些例子。所以,到目前为止我已经编写了以下代码:

    Sub Try()
         Dim cn As New ADODB.Connection
         Dim rs As ADODB.Recordset
         Dim cmd As ADODB.Command
         Dim chunk() As Byte
         Dim fd As Integer
         Dim flen As Long
         Dim Main As ADODB.Parameter
         Dim object As ADODB.Parameter

     Stil = vbYesNo + vbCritical + vbDefaultButton1
        Titel = "db connection test"
    '   Meldung anzeigen.
        Antwort = MsgBox("trying to connect to db", Stil, Titel, Hilfe, Ktxt)

         ' Connect to the database using ODBC [msdaora][ORAOLEDB.Oracle]Provider=ORAOLEDB.Oracle;
         With cn
             .ConnectionString = "Provider=ORAOLEDB.Oracle;Password=pass;User ID=usr;Data Source=host:port:sid"
             .Open
             .CursorLocation = adUseClient
         End With

         ret = cn.Execute("create table newtesttable (main integer, object oid)")

         ' Here is an example if you want to issue a direct
    ' command to the database
         '
         'Set cmd = New ADODB.Command
         'With cmd
         '    .CommandText = "delete from MYTABLE"
         '    .ActiveConnection = cn
         '    .Execute
         'End With
         'Set cmd = Nothing

         '
         ' Here is an example of how insert directly into the
     ' database without using
         ' a recordset and the AddNew method
         '
         Set cmd = New ADODB.Command
         cmd.ActiveConnection = cn
         ' cmd.CommandText = "insert into newtesttable(main,object) values(?,?)"
         cmd.CommandText = "select * from test"
         cmd.CommandType = adCmdText

         ' The main parameter
        ' Set main = cmd.CreateParameter("main", adInteger, adParamInput)
         'main.Value = 100 '' a random integer value ''
         'cmd.Parameters.Append main

         ' Open the file for reading
         'fd = FreeFile
         'Open "myBlobFile.txt" For Binary Access Read As fd
         'flen = LOF(fd)
         'If flen = 0 Then
          '   Close
           '  MsgBox "Error while opening the file"
            ' End
         'End If

         ' The object parameter
         '
         ' The fourth parameter indicates the memory to allocate
     ' to store the object
       '  Set object = cmd.CreateParameter("object", _
       '                                       adLongVarBinary, _
       '                                       adParamInput, _
                                              flen + 100)
       '  ReDim chunk(1 To flen)
       '  Get fd, , chunk()

         ' Insert the object into the parameter object
       '  object.AppendChunk chunk()
       '  cmd.Parameters.Append object

         ' Now execute the command
         Set rs = cmd.Execute

     '   Mldg = "test"
        Stil = vbYesNo + vbCritical + vbDefaultButton1
        Titel = "asdasdasd"
    '   Meldung anzeigen.
        Antwort = MsgBox(rs, Stil, Titel, Hilfe, Ktxt)
         ' ... and close all
         cn.Close
         Close

 End Sub

我相信这段代码有很多问题,但目前它在尝试执行.Open时失败,说"Provider cannot be found. It may not be properly installed"。之后我发现我需要下载并安装 ORAOLEDB.dll。我通过安装 ORAOledb11.dll 来做到这一点(我尝试了 32 位和 64 位,你的机器是 64 位的)。我已经通过执行regsvr32 OraOLEDB11.dll 安装了它。

不幸的是,问题一直存在。那么,解决此问题的步骤是什么? 我能否以某种方式确保 Oraoledb 已正确安装在我的机器上?

任何提示将不胜感激。

【问题讨论】:

  • 我相信这应该会有所帮助:dzone.com/snippets/excel-make-query-oracle 通常,将 Oracle 连接到 Excel 比例如复杂得多。 “本机”MSSQL。祝你好运!
  • 几个月前对我有帮助的另外一个链接:blog.mclaughlinsoftware.com/microsoft-excel/…
  • 进入 Regedit 并查看密钥 HKEY_CLASSES_ROOT\OraOLEDB.Oracle 是否存在。安装 Oracle 客户端时默认不安装 OLEDB 驱动程序(您不是已经安装了 Oracle 客户端吗?)。顺便说一句,这种语法也是错误的,数据源是 TNSNAMES 文件中引用您的数据库的条目。但这不会给你你得到的错误。
  • 感谢您的回答。我确实在注册表中有上述密钥。

标签: excel oracle vba odbc oledb


【解决方案1】:

32 位操作系统

通过从 Oracle 官方网站 Oracle10g Provider for OLE DB Version 10.1.0.4.0 下载 oracle OLEDB 提供程序,我设法在 Windows XP 虚拟机上运行。 当前工作的 link OLEDB 用于旧操作系统(32 位)

但请注意,它会将 JDK 和 JRE 替换为较低版本(可以通过玩配置 xml - products.xml - 我没有足够的心理健康药水,所以我而是进行了完整安装)。之后您需要删除环境变量中的引用,因为它可能会影响其他程序。 安装后,我用 regsvc32

注册了 OraOLEDBxx.dll

我正在使用 excel 2003 连接到 oracle db 11G。:)

连接字符串

我必须启用扩展(ActiveX 数据对象和记录库)。我的函数返回连接是:

Public Function connectToDb(provider As String, host As String, sid As String, user As String, pwd As String, db As String) As ADODB.Connection
Dim conn As ADODB.Connection
Dim dbConnectStr As String

    Set conn = New ADODB.Connection
    If provider = "Oracle" Then
        dbConnectStr = "Provider=OraOLEDB.Oracle;Data Source=" & host & ":1521/" & sid & ";User Id=" & user & ";Password=" & pwd & ";"
    Else

    End If
    conn.ConnectionString = dbConnectStr
    conn.Open
    Set connectToDb = conn
End Function

64 位操作系统但 32 位 Office

当我们的虚拟机迁移到带有 Excel 2010 的 64 位 Windows 7 时。确保您将下载 ODAC - Oracle 数据访问组件 - 用于您的 excel 安装的正确 -bit 版本,因为我安装了 32 位 excel 并认为它是 64 位(因为 windows 是 64 位的)所以我正在分娩试图让它与 64 位 ODAC 版本一起工作。之后我确实下载了 32 位版本,它和以前一样工作。只需按照下载的存档文件夹中包含的安装说明进行安装即可。

目前在 Oracle 网站上为 ODAC 工作的 links

【讨论】:

  • 感谢您的详细解答。不幸的是,我不能再测试它了:)因此接受为正确答案,但无论如何都赞成;)
  • @AlexK。我对此感到满意..我只是想分享我近一周的挣扎..我在这里遇到了同样的错误,当我试图将 64 位 DLL 与 32 位 excel 一起使用时..
  • 成功了,并且没有数据源:dbConnectStr = "Provider=OraOLEDB.Oracle;Data Source=" & host & ":1521/" & sid & ";User Id=" & user & ";密码=" & pwd & ";" - 非常感谢
【解决方案2】:

您可以通过创建一个以 .udl 结尾的文本文件,然后关闭并打开该文件来确保您的连接字符串是准确的。系统将提示您使用用户界面连接到服务器。输入您的信息并测试连接。然后,如果您的连接正常,请关闭该文件。以文本格式打开该文件并将连接字符串复制到您的代码中。还要确保为 ADO 选择了您的参考库。这行看起来不对:

Data Source=host:port:sid

下面是我用来从文本中提取 sql 并将结果提取到文本的示例。

 Public Function ObjectConnect(AID As String, APswd As String)

 ObjectConnect = "Provider=ORAOLEDB;Password=" & APswd & ";Persist Security Info=True;User ID=" & AID & ";Data Source=(nameofserverConn)"

 End Function

Sub RunSQL()

Dim strConn As String
Dim Query As String
Dim txt As Object
Dim ns As ADODB.Connection
Dim rs As ADODB.Recordset
Dim txtfile As Object
Dim f As ADODB.Field
Dim myFileSystemObject As Object
Dim txtN As String
Dim DL As String


FName1 = ""
Query = ""
txtStrngX = ""

Set ns = New ADODB.Connection
Set rs = New ADODB.Recordset
ns.ConnectionTimeout = 99000
ns.CommandTimeout = 99000



ns.Open ObjectConnect('UserID', 'Password') 'this is a public function w. userform for people to enter ID and Password.
With rs
    .ActiveConnection = ns
      'however you're writing the sql it would go here.
    .Open Query 
End With


If rs.State <> 0 Then
    DL = Sheet1.Cells(2, 2)
    RecordsetToText rs:=rs, FullPath:=txtN, ValueDelimiter:=DL  'this is a sub function that writes to a text file.  Didn't include the code but left this.  Here you would want to do something with the recordset.
End If

On Error Resume Next
rs.Close
Set rs = Nothing


End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 2012-06-06
    • 1970-01-01
    相关资源
    最近更新 更多