OTL 4.0, Example 365 (#define OTL_UNICODE_EXCEPTION_AND_RLOGON)

Source Code

// One of the following two #define's enables ODBC / DB2 CLI 
// Unicode SQLxxxW functions
#define UNICODE
#define _UNICODE

#include <iostream>
using namespace std;

#include <stdio.h>
#define OTL_ODBC_MSSQL_2008 // Compile OTL 4/ODBC, MS SQL 2008
//#define OTL_ODBC // Compile OTL 4/ODBC. Uncomment this when used with MS SQL 7.0/ 2000
#define OTL_UNICODE // Enable Unicode OTL for ODBC

// Enable OTL Unicode rlogon and otl_exception for ODBC
#define OTL_UNICODE_EXCEPTION_AND_RLOGON

#if defined(__GNUC__)
#define OTL_UNICODE_CHAR_TYPE SQLWCHAR
#else
#define OTL_UNICODE_CHAR_TYPE wchar_t
#endif

#include <otlv4.h> // include the OTL 4.0 header file

otl_connect db; // connect object

int main()
{
 otl_connect::otl_initialize(); // initialize the database API environment
 try{
// Special Unicode rlogon function
  db.rlogon(L"scott",
L"tiger",
L"mssqlxxx"
); // connect to the database

 }

 catch(otl_exception& p){ // intercept OTL exceptions
#if defined(__GNUC__)
OTL_UNICODE_CHAR_TYPE* c=p.msg;
while(*c){
cerr<<(char)*c;
++c;
}
cerr<<endl;
c=p.sqlstate;
while(*c){
cerr<<(char)*c;
++c;
}
cerr<<endl;
#else
  wcerr<<p.msg<<endl; // print out error message
  wcerr<<p.sqlstate<<endl; // print out error sqlstate
  wcerr<<p.stm_text<<endl; // print out SQL that caused the error
  wcerr<<p.var_info<<endl; // print out the variable that caused the error
#endif
 }

 db.logoff(); // disconnect from the database

 return 0;

}

Output

[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
IM002


Examples ContentsGo Home

Copyright © 1996-2024, Sergei Kuchin, email: skuchin@gmail.com, skuchin@gmail.com .

Permission to use, copy, modify and redistribute this document for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.