首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel VBA用InstantClient连接远程Oracle DB

Excel VBA用InstantClient连接远程Oracle DB
EN

Stack Overflow用户
提问于 2014-03-03 18:20:58
回答 1查看 27.5K关注 0票数 4

我试图使用Excel (主要是2003,以获得更多的用户兼容性)连接到远程Oracle DB。我想运行一个.sql脚本并将数据集返回到工作表中。

我在一台Windows 7 64位机器上。我不知道Oracle DB服务器的规范。

我希望保持尽可能轻量级(在客户端机器上没有额外的文件安装,尽可能多地使用共享网络位置来处理所需的文件)。

迄今为止:

我从Oracle (32位和64位版本的12.1和11.2版本)下载并“安装”到远程网络位置。

我尝试使用SQL连接到Oracle,它运行良好(我尝试了几个已安装的InstantClient版本,以查看是否存在任何兼容性问题)。

作为一个测试:在VBA中使用SQL和Shell函数,我成功地将数据放入一个单独的excel文件中。

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

  • Driver={instantclient_11_2中的Oracle}
  • Driver={}
  • Provider=MSDAORA
  • Provider=MSDAORA.1
  • Provider=OraOLEDB.Oracle

我收到的错误:

代码语言:javascript
复制
"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..."
代码语言:javascript
复制
"Run-time error '-2147467259 (80004005)':
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"
代码语言:javascript
复制
"Run-time error '3706':
Provider cannot be found. It may not be properly installed"

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

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

我需要:

TNS_ADMIN?ORACLE_HOME?

问题:

  • 如何使用位于网络(共享)位置的实例化客户端文件使用VBA连接远程Oracle DB

代码语言:javascript
复制
- 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)?) 

我的EZConnect格式:EZConnect

代码语言:javascript
复制
- What "provider" or "driver" _should_ I use for this purpose and are there any significant differences?
代码语言:javascript
复制
- What environmental variables do I **require** to make this work?

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-12 17:42:28

最后编辑/使用此函数(而不是(?)使用驱动程序/提供程序:InstantClient,但仍然使用这些文件):

代码语言:javascript
复制
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

正确的全连接字符串:

代码语言:javascript
复制
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环境变量以指向实例化客户端。

没有使用任何其他环境变量,如ORACLE_HOME、TNS_ADMIN等。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22154288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档