C# Connect to Oracle DataBase Error ORA-12545: Connect failed because target host or object does not exist

*爱你&永不变心* 提交于 2020-01-05 06:28:53

问题


I want to connect to Oracle database from C# but I can't Because this errors that ORA-12545: Connect failed because target host or object does not exist so how can i ?

C# code

  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) (PRESENTATION = RO) ) )

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


回答1:


The error

ORA-12454: connect failed because target host or object does not exist

It's mean you have problem with connection string, check all access data, machine name once again and try use this connection string, you forgot add "ADDRESS_LIST" , check doc. here.

Using ODP.NET without tnsnames.ora, should be like

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

So you connection string should look like

   string strCon =@"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=DESKTOP-EIQ5MRF)(PORT=1521)))
                             (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XE)));
                             User Id = system; Password = bluesky; ";


来源:https://stackoverflow.com/questions/40717481/c-sharp-connect-to-oracle-database-error-ora-12545-connect-failed-because-targe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!