【问题标题】:Excel VBA connect to remote Oracle DB with InstantClientExcel VBA 使用 InstantClient 连接到远程 Oracle 数据库
【发布时间】:2014-04-04 22:41:17
【问题描述】:


我正在尝试使用 Excel(主要是 2003,以提高用户兼容性)连接到远程 Oracle 数据库。我想运行 .sql 脚本并将数据集返回到工作表。
我在 Windows 7 64 位机器上。我不知道 Oracle DB 服务器的规格。
我想尽可能保持轻量级(在客户端计算机上没有额外的文件安装,尽可能为所需文件使用共享网络位置)



到目前为止:

我从 Oracle 下载 InstantClient(32 位和 64 位版本 12.1 和 11.2)并将其“安装”到远程网络位置。
我尝试使用 SQL Plus 连接到 Oracle DB,它工作正常(我尝试了几个已安装的 InstantClient 版本,看看是否会有任何兼容性问题)。
作为测试:在 VBA 中使用 SQL Plus 和 Shell 函数,我能够成功地将数据假脱机到一个单独的 excel 文件中。


我使用各种驱动程序/提供程序尝试了几种不同的连接字符串格式:

  • Driver={Oracle in instantclient_11_2}
  • 驱动程序={Microsoft ODBC for Oracle}
  • 提供者=MSDAORA
  • Provider=MSDAORA.1
  • 提供者=OraOLEDB.Oracle

我收到的错误:

"Run-time error '-2147467259 (80004005)':
[Microsoft][ODBC Driver Manager] Driver's SQLAllocHandle on SQL_HANDLE_ENV failed
The Oracle(tm) client and networking components were not found. These components are supplied by Oracle Corporation..."


"Run-time error '-2147467259 (80004005)':
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"


"Run-time error '3706':
Provider cannot be found. It may not be properly installed"

还有一些其他类似的错误。



我已将包含即时客户端文件的网络位置添加到我的 PATH 环境变量中。不确定我需要哪些其他环境变量,或者即使我当前的环境变量是否正确。

我需要吗:
TNS_ADMIN? ORACLE_HOME?



问题:

  • 如何使用位于 网络(共享)位置的 instantclient 文件通过 VBA 连接到 远程 Oracle DB ?

    • 什么是正确的完整连接字符串? (我在 SQLPlus 中使用了 EZConnect 格式;实际的连接细节是否相同?为了澄清起见,有人可以发布一个 EZConnect 格式如何转换为其他格式的示例吗?)

      My EZConnect Format: username/password@myserver.some.thing.com/mydb
      
    • 我应该为此目的使用什么“提供者”或“驱动程序”,是否存在显着差异?

    • 需要哪些环境变量才能完成这项工作?

我发现了很多相似或相关的问题,但没有一个能直接回答我的问题或帮助我完全解决问题。

【问题讨论】:

    标签: sql excel oracle vba instantclient


    【解决方案1】:

    最终编辑/使用此功能(不(?)使用驱动程序/提供程序:InstantClient,但仍使用文件):

    Function ORAQUERY(strHost As String, strDatabase As String, strSQL As String, strUser As String, strPassword As String)
      Dim strConOracle, oConOracle, oRsOracle
      Dim StrResult As String
      StrResult = ""
      strConOracle = "Driver={Microsoft ODBC for Oracle}; " & _
             "CONNECTSTRING=(DESCRIPTION=" & _
             "(ADDRESS=(PROTOCOL=TCP)" & _
             "(HOST=" & strHost & ")(PORT=1521))" & _
             "(CONNECT_DATA=(SERVICE_NAME=" & strDatabase & "))); uid=" & strUser & " ;pwd=" & strPassword & ";"
      Set oConOracle = CreateObject("ADODB.Connection")
      Set oRsOracle = CreateObject("ADODB.Recordset")
      oConOracle.Open strConOracle
      Set oRsOracle = oConOracle.Execute(strSQL)
      MsgBox (oRsOracle.Fields(0).Value)
      varResult = oRsOracle.GetRows
      Do While Not oRsOracle.EOF
          If StrResult <> "" Then
            StrResult = StrResult & Chr(10) & oRsOracle.Fields(0).Value
          Else
            StrResult = oRsOracle.Fields(0).Value
          End If
        oRsOracle.MoveNext
      Loop
      oConOracle.Close
      Set oRsOracle = Nothing
      Set oConOracle = Nothing
      ORAQUERY = StrResult
    End Function
    



    正确的完整连接字符串:

    Driver={Microsoft ODBC for Oracle}; CONNECTSTRING=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=strHost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=strDatabase))); uid=strUser; pwd=strPassword;
    

    提供者或驱动程序:
    {Microsoft ODBC for Oracle}

    需要将 PATH 环境变量设置为指向 Instantclient。
    没有使用任何其他环境变量,例如ORACLE_HOME、TNS_ADMIN 等

    【讨论】:

    • 安装 Visual Studio 2015 使我的道路变得不堪重负,这破坏了我所有的 Sybase 和 Oracle 提供程序。将 Instantclient 添加回路径修复了 MSDAORA 客户端(所以,谢谢)。微软开发者们,你们到底在想什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    相关资源
    最近更新 更多