使用OCCI连接Oracle数据库遇报错求助
Hey there, let's tackle those ORA errors you're hitting while trying to connect to Oracle via OCCI in Visual Studio 2017. I've worked through similar connection issues before, so here's what to check and fix:
1. Fix the Syntax in Your Connection Descriptor
The first red flag is the extra single quotes around parameter values in your connection string. Oracle doesn’t require (or expect) quotes around values like TCP, my.host.cz, or 1510 in the TNS descriptor. These quotes add unnecessary length (triggering ORA-12163) and can cause parsing issues that lead to ORA-12560.
Here’s the corrected code with quotes removed:
Environment *env = Environment::createEnvironment(Environment::DEFAULT); Connection *con; con = env->createConnection(Config::login, Config::password, "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = my.host.cz)(PORT = 1510))(CONNECT_DATA = (SID = mySid)))");
2. Simplify with a TNS Alias (Avoid Long Descriptors)
If the corrected string still throws ORA-12163, using a TNS alias is a reliable fix. This replaces the long descriptor with a short name, eliminating length issues and reducing formatting errors:
- Open your
tnsnames.orafile (usually located inORACLE_HOME\network\adminor the path set by theTNS_ADMINenvironment variable). - Add an entry for your database:
MY_DB_ALIAS = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = my.host.cz)(PORT = 1510)) (CONNECT_DATA = (SID = mySid) ) ) - Update your code to use the alias:
con = env->createConnection(Config::login, Config::password, "MY_DB_ALIAS");
3. Troubleshoot ORA-12560 (Protocol Adapter Error)
This error often stems from mismatched configurations rather than the connection string itself:
- Platform Bitness Match: Ensure your VS project’s target platform (x86/x64) exactly matches the bitness of your Oracle Client installation. For example, a 64-bit client requires a VS project set to x64.
- Environment Variables: Verify
ORACLE_HOMEpoints to your Oracle Client installation directory, andTNS_ADMIN(if used) points to the folder containingtnsnames.ora. Restart VS after changing these variables to apply the settings. - Client-Server Compatibility: Use an OCCI library version that’s compatible with your Oracle Server version. Sticking to the same major version (e.g., 12c client for a 12c server) avoids compatibility gaps.
4. Validate Connection with sqlplus First
Before debugging code, test your connection using Oracle’s sqlplus tool to rule out network or credential issues:
sqlplus your_username/your_password@(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = my.host.cz)(PORT = 1510))(CONNECT_DATA = (SID = mySid)))
If sqlplus fails to connect, fix the underlying network/credential problem first. If it works, the issue lies in your VS project’s OCCI configuration.
内容的提问来源于stack exchange,提问作者Evka'S

