SQLConnect() - ODBC API - Oracle to SQL Server and SQL Azure Migration

SQLConnect function establishes the connection to a database using an existing ODBC data source, user name and password.

Quick Example:

   // Connect to Oracle
   SQLRETURN retcode = SQLConnect(hdbc, "oracle_dsn", SQL_NTS, "scott", SQL_NTS, "tiger", SQL_NTS);

Overview

Conversion issues:

Oracle SQL Server and SQL Azure
ODBC Datasource May require change
User and Password May require change
Return on Success SQL_SUCCESS SQL_SUCCESS or SQL_SUCCESS_WITH_INFO
Return on Error SQL_ERROR SQL_ERROR
Error Check Code if(ret == SQL_SUCCESS) May require change
if(retcode != SQL_SUCCESS) May require change
if(retcode != SQL_ERROR)
if(retcode == SQL_ERROR)
if(retcode >= SQL_SUCCESS)
if(retcode => 0)
if(retcode <= -1)

Last Update: Microsoft SQL Server 2012 and Oracle 11g

Conversion Details

When you connect to Oracle using Oracle ODBC driver (sqora32.dll), the SQLConnect() function returns SQL_SUCCESS when the connection is successful.

At the same time, if you use SQL Server Native Client (native SQL Server ODBC driver) to connect to SQL Server, it returns SQL_SUCCESS_WITH_INFO when the connection is successful, and the message text contains:

"[Microsoft][SQL Server Native Client 11.0][SQL Server]Changed database context to 'master'."

Note. SQL_SUCCESS_WITH_INFO is returned even if you do not specify to change the default database in the SQL Server ODBC driver settings.

Error Code Check

If your Oracle application checks SQL_SUCCESS after calling SQLConnect(), you have to change the code to also check for SQL_SUCCESS_WITH_INO, or just check for a value >= 0 for a successful connection, and < 0 for failed connection:

Oracle:

   // Connect to Oracle
   SQLRETURN retcode = SQLConnect(hdbc, "db_dsn", SQL_NTS, "scott", SQL_NTS, "tiger", SQL_NTS);
 
   // Check if connection failed
   if(retcode != SQL_SUCCESS)
   {
      // ... exit
   }
 
   // Check if connection successful
   if(retcode == SQL_SUCCESS)
   {
      // ... continue processing
   }

SQL Server and SQL Azure:

   // Connect to SQL Server
   SQLRETURN retcode = SQLConnect(hdbc, "db_dsn", SQL_NTS, "scott", SQL_NTS, "tiger", SQL_NTS);
 
   // Check if connection failed
   if(retcode < SQL_SUCCESS)   // changed != to < 
   {
      // ... exit
   }
 
   // Check if connection successful
   if(retcode >= SQL_SUCCESS)    // changed == to >=
   {
      // ... continue processing
   }

SQLines ODBC Driver for Oracle to SQL Server

SQLines ODBC driver supports the following conversion features for SQLConnect() function:

  • Automatically changes return code SQL_SUCCESS_WITH_INFO to SQL_SUCCESS
  • Provides options to maps the data source name, user name and password

Using SQLines ODBC driver you do not need to modify existing Oracle ODBC applications that use SQLConnect() function. More...

Resources