VBA到Oracle 10g错误

时间:2013-07-27 18:23:39

标签: oracle vba excel-vba excel

问候..

我对Oracle很新,但对Sql有所了解。

我正在尝试从VBA连接外部Oracle数据库 - 下面的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:Cound无法解析指定的连接标识符

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

enter image description here

请帮助我。提前感谢您的帮助..

1 个答案:

答案 0 :(得分: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 - 请记住激活工具 - >参考 - >“Microsoft ActiveX数据对象2.8库”