0

问候..

我对 Oracle 很陌生,但对 Sql 有一定的了解。

我正在尝试从 VBA 连接外部 Oracle DB - 下面的 Excel 是代码..

 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_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=207.169.236.29)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=DEDWRP01.DE.EDS.COM))); uid=" & strUser & " ;pwd=" & strPassword & ";"
 Set oConOracle = CreateObject("ADODB.Connection")
 Set oRsOracle = CreateObject("ADODB.Recordset")
 oConOracle.Open strConOracle
 End Function

在尝试运行上面的代码时,我遇到了错误..

[Microsoft][Oracle 的 ODBC 驱动程序][Oracle]ORA-12154:TNS:无法解析指定的连接标识符

我已经安装了 oracle 10g,我可以通过Oracle SQL* Plus连接数据库。

在此处输入图像描述

请帮助我。谢谢提前帮助..

4

1 回答 1

0

这是我使用的实时代码:

Dim oCon As ADODB.Connection
Dim oRS As ADODB.Recordset
Set oCon = New ADODB.Connection
oCon.ConnectionString = "DSN=YOUR_DNS;UID=YOUR_USERNAME;PWD=YOUR_PASSWORD;APP=Microsoft Office 2003;WSID=YOUR_WSID"
oCon.Open
Set oRS = New ADODB.Recordset
oRS.ActiveConnection = oCon

'Loop through Oracle
    oRS.Source = "select DATA from TABEL;"
    oRS.Open

        Do While Not oRS.EOF
            Debug.Print oRS.Fields("DATA").Value
            oRS.MoveNext
        Loop

    oRS.Close

oCon.Close


If Not oRS Is Nothing Then Set oRS = Nothing
If Not oCon Is Nothing Then Set oCon = Nothing

连接到 Oracle - 记得激活 Tools->References->"Microsoft ActiveX Data Objects 2.8 Library"

于 2013-07-28T10:57:32.453 回答