简体   繁体   English

Excel VBA使用InstantClient连接到远程Oracle DB

[英]Excel VBA connect to remote Oracle DB with InstantClient


I am trying to use Excel (mainly 2003, for more user compatibility) to connect to a remote Oracle DB. 我正在尝试使用Excel(主要是2003,以获得更多用户兼容性)来连接到远程Oracle DB。 I would like to run a .sql script and return the dataset to a worksheet. 我想运行.sql脚本并将数据集返回到工作表。
I am on a Windows 7 64bit machine. 我在Windows 7 64位机器上。 I do not know the specs of the Oracle DB server. 我不知道Oracle DB服务器的规格。
I would like to keep this as lightweight as possible (no extra file installations on client machines, use shared network locations for required files as much as possible) 我想保持尽可能轻量级(在客户端计算机上没有额外的文件安装,尽可能使用共享网络位置来获取所需文件)



So Far: 至今:

I downloaded and "installed" the InstantClient from Oracle (versions 12.1 and 11.2 for both 32bit and 64bit) onto a remote network location. 我将来自Oracle的InstantClient(32位和64位版本12.1和11.2)下载并“安装”到远程网络位置。
I tried connecting to the Oracle DB using SQL Plus and it worked fine (I tried several of the installed InstantClient versions to see if there would be any compatibility issues). 我尝试使用SQL Plus连接到Oracle数据库并且工作正常(我尝试了几个已安装的InstantClient版本以查看是否存在任何兼容性问题)。
As a test: using SQL Plus and the Shell function in VBA, I was able to successfully spool the data into a separate excel file. 作为测试:在VBA中使用SQL Plus和Shell函数,我能够成功将数据假脱机到一个单独的excel文件中。


I tried several different connection string formats using various drivers/providers: 我使用各种驱动程序/提供程序尝试了几种不同的连接字符

  • Driver={Oracle in instantclient_11_2} Driver = {Oracle in instantclient_11_2}
  • Driver={Microsoft ODBC for Oracle} Driver = {Microsoft ODBC for Oracle}
  • Provider=MSDAORA 供应商= MSDAORA
  • Provider=MSDAORA.1 供应商= MSDAORA.1
  • Provider=OraOLEDB.Oracle 供应商= OraOLEDB.Oracle

Errors I Received: 我收到的错误:

"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"

And a few other similar errors. 还有一些其他类似的错误。



I have added the network location containing the instantclient files to my PATH environmental variable. 我已将包含instantclient文件的网络位置添加到我的PATH环境变量中。 Not sure what other environmental variables I require or even if my current one is correct. 不确定我需要的其他环境变量,或者即使我当前的变量是正确的。

Do I need: 我需要:
TNS_ADMIN? TNS_ADMIN? ORACLE_HOME? ORACLE_HOME?



Question: 题:

  • How do I connect to the remote Oracle DB with VBA, using the instantclient files that are located in a network (share) location ? 如何使用位于网络(共享)位置instantclient文件使用VBA连接到远程Oracle DB

    • What is the correct full connection string? 什么是正确的完整连接字符串? (I used the EZConnect format with SQLPlus; are the actual connection details the same? and for clarification, could someone post an example of how the EZConnect format converts to the other format(s)?) (我在SQLPlus中使用了EZConnect格式;实际的连接细节是否相同?为了澄清,有人可以发布一个EZConnect格式如何转换为其他格式的示例吗?)

       My EZConnect Format: username/password@myserver.some.thing.com/mydb 
    • What "provider" or "driver" should I use for this purpose and are there any significant differences? 应该为此目的使用什么“提供者”或“驱动程序”,是否存在重大差异?

    • What environmental variables do I require to make this work? 需要哪些环境变量来完成这项工作?

I've found a lot of questions that are similar or related, but none that directly answered my question or helped me enough to completely solve it. 我发现了许多类似或相关的问题,但没有一个直接回答我的问题或帮助我完全解决它。

Ended up editing/using this function (which does not(?) use driver/provider: InstantClient but still uses the files): 结束编辑/使用此功能(不使用(?)使用驱动程序/提供程序: 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



Correct full Connection String: 正确的完整连接字符串:

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

Provider or Driver: 提供者或司机:
{Microsoft ODBC for Oracle} {Microsoft ODBC for Oracle}

Needed to set PATH environmental variable to point to instantclient. 需要将PATH环境变量设置为指向instantclient。
Didn't use any of the other environmental variables eg ORACLE_HOME, TNS_ADMIN, etc. 没有使用任何其他环境变量,例如ORACLE_HOME,TNS_ADMIN等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM