使用完整的连接字符串和EZCONNECT绕过tnsnames.ora时,出现“ ORA-12560:TNS:协议适配器错误”

时间:2018-08-30 12:11:05

标签: oracle oracle11g odbc pyodbc tnsnames

尝试使用任一连接字符串绕过tnsnames.ora(完整的连接字符串和EZCONNECT)时,尝试连接数据库时会返回以下错误:

  

pyodbc.Error :(“ HY00”,“ [HY00] [Oracle] [ODBC] [ORA-12560:TNS:协议适配器错误\ n(12560)(SQLDriverConnect); [Oracle] [ODBC] [ Ora] ORA-12560:TNS:协议适配器错误\ n(12560)')

我尝试使用https://www.connectionstrings.com/oracle/中的以下连接字符串:

Driver={Oracle in OraClient11g_home1}; Data Source=uid/pwd@//host:port/service_name;

Driver={Oracle in OraClient11g_home1};
Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host)(PORT=port))) 
                         (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=service_name)));
User Id=uid;Password=pwd;'

我可以使用sqlplus和EZCONNECT连接到数据库:

PS: C:\>sqlplus uid/pwd@//host:port/service_name

SQL*PLUS: Release 11.2.0.1.0 Production on Thu Aug 30 11:56:22 2018

Copyright (c) 1982, 2010, Oracle. All rights reserved.

Connected to:
Oracle Database 12c Standard Edition Release 12.1.0.2.0 - 64bit Production

SQL>

我怀疑这是驱动程序问题,因为使用其他驱动程序会返回不同的错误:

  

pyodbc.InterfaceError :(“ IM002”,“ [IM002] [Microsoft] [ODBC驱动程序管理器]数据源名称未找到且未指定默认驱动程序(0)(SQLDriverConnect)”)

为什么连接字符串适用于sqlplus但不适用于ODBC?

1 个答案:

答案 0 :(得分:2)

使用Oracle ODBC驱动程序时,连接字符串应如下所示:

Driver={Oracle in OraClient11g_home1};
DBQ={(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host)(PORT=port))) 
                         (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=service_name)))};
Uid=uid;Pwd=pwd;

请参见Oracle in OraClient11g_home1 connection strings

对于deprecated Microsoft驱动程序,应该是

Driver={Microsoft ODBC for Oracle};
Server={(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host)(PORT=port))) 
                         (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=service_name)))};
Uid=uid;Pwd=pwd;

请参见Microsoft ODBC for Oracle connection strings

请注意, Microsoft ODBC for Oracle 仅适用于32位