C#连接到Oracle DataBase错误ORA-12545:连接失败,因为目标主机或对象不存在

时间:2016-11-21 10:04:01

标签: c# oracle11g database-connection

我想从C#连接到Oracle数据库,但我不能 因为ORA-12545:连接失败的错误,因为目标主机或对象不存在 那我该怎么办?

C#代码

  string strConn = "Data Source =(DESCRIPTION =" +
                                 "(ADDRESS = (PROTOCOL = TCP)(HOST = DESKTOP-EIQ5MRF)(PORT = 1521))" +
                                 "(CONNECT_DATA =" +
                                 " (SERVER = DEDICATED)" +
                                 "(SERVICE_NAME = XE)" +
                                 ")" +
                                 ");User Id =system;password=bluesky;";
                OracleConnection conn = new OracleConnection(strConn);

                conn.Open();
                conn.Close();

listener.ora

  

SID_LIST_LISTENER =(SID_LIST =       (SID_DESC =         (SID_NAME = PLSExtProc)         (ORACLE_HOME = C:\ oraclexe \ app \ oracle \ product \ 11.2.0 \ server)         (PROGRAM = extproc)       )       (SID_DESC =         (SID_NAME = CLRExtProc)         (ORACLE_HOME = C:\ oraclexe \ app \ oracle \ product \ 11.2.0 \ server)         (PROGRAM = extproc)       ))

     

LISTENER =(DESCRIPTION_LIST =       (DESCRIPTION =         (ADDRESS =(PROTOCOL = IPC)(KEY = EXTPROC1))         (ADDRESS =(PROTOCOL = TCP)(HOST = DESKTOP-EIQ5MRF)(PORT = 1521))       ))

     

DEFAULT_SERVICE_LISTENER =(XE)

     

的tnsnames.ora

     

XE =(DESCRIPTION =       (ADDRESS =(PROTOCOL = TCP)(HOST = DESKTOP-EIQ5MRF)(PORT = 1521))       (CONNECT_DATA =         (SERVER = DEDICATED)         (SERVICE_NAME = XE)       ))

     

EXTPROC_CONNECTION_DATA =(DESCRIPTION =       (ADDRESS_LIST =         (ADDRESS =(PROTOCOL = IPC)(KEY = EXTPROC1))       )       (CONNECT_DATA =         (SID = PLSExtProc)         (演示= RO)       ))

     

ORACLR_CONNECTION_DATA =(DESCRIPTION =       (ADDRESS_LIST =         (ADDRESS =(PROTOCOL = IPC)(KEY = EXTPROC1))       )       (CONNECT_DATA =         (SID = CLRExtProc)         (演示= RO)       ))

1 个答案:

答案 0 :(得分:1)

错误

  

ORA-12454:连接失败,因为目标主机或对象不存在

这意味着您遇到连接字符串问题,再次检查所有访问数据,机器名称并尝试使用此连接字符串,您忘记添加" ADDRESS_LIST" ,在这里查看doc.

使用不ODP.NET的{​​{1}},应该像

tnsnames.ora

所以连接字符串应该看起来像

    var sampleConn= @"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=    (PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))
                    (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));
                     User Id=myUsername;Password=myPassword;"