DRAFT

C++ Omnidex ODBC Simple Connection/SELECT

Quick Notes

  • SQLConnect can only use System and Machine datasources so use SQLDriverConnect.
  • SQLDriverConnect can use a Omnidex File DSN with the FILEDSN= prefix.
  • Make sure odxodbc.dll is in the c:\Windows\system32 directory
  • Make sure an OdxNet process is running

C++ Sample Code

You can cut and paste this code and modify the the SQLDriverConnection call and the SELECT statement to test on your Omnidex Environment.

#include "stdafx.h"
#include <windows.h>
#include <sql.h>
#include <sqltypes.h>
#include <sqlext.h>
#include <iostream>
using namespace std;
#pragma warning(disable: 4996)
 
int main() 
{
  SQLHENV henv;
  SQLHDBC hdbc;
  SQLHSTMT hstmt;
  char errormsg[255] = "x";
  SQLCHAR * OutConnStr = (SQLCHAR * )malloc(255);
  SQLSMALLINT * OutConnStrLen = (SQLSMALLINT *)malloc(255);
 
  cout << "OdxODBCConnectionTest" << endl;
  // Allocate environment handle
  if (SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv) != SQL_SUCCESS)
    cout << "sQLAllocHandle (ENV) Error" << endl;
  if (SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC2, 0) != SQL_SUCCESS)
	cout << "SQLSetEnvAttr Error" << endl;
  if (SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc) != SQL_SUCCESS) 
    cout << "SQLAllocHandle (DBC) Error" << endl;
  if (SQLDriverConnect(hdbc, NULL,
	     (SQLCHAR*) "FILEDSN=c:\\dev\\odx\\tiny\\tiny.dsn", SQL_NTS,
		 (SQLCHAR*) NULL, 0, NULL, 0) != SQL_SUCCESS) 
  {	
     cout << "SQLDriverConnect Error:" << endl;
     // If we get to here, display the SQLError message text to see what’s going on.
     SQLError(henv, hdbc, NULL, NULL , NULL, (SQLCHAR*) errormsg, sizeof(errormsg), NULL);
     cout  << errormsg << endl;
  }
  if (SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt) != SQL_SUCCESS) 
    cout << "SQLAllocHandle (STMT) Error" << endl;
  if (SQLExecDirect(hstmt, 
	(SQLCHAR*) "select mytext from table1 where mytext='bears'", SQL_NTS) != SQL_SUCCESS)
    cout << "ExecDirect Error" << endl;
  SQLCHAR mytext[60];  // Declare varable for the Bind.
  if (SQLBindCol(hstmt, 1, SQL_C_CHAR, (SQLPOINTER) &mytext,
    sizeof(mytext), NULL) != SQL_SUCCESS)
	cout << "SQLBindCol Error" << endl;
  while (SQLFetch(hstmt) == SQL_SUCCESS)
    cout << mytext << endl;
  SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
  if (SQLDisconnect(hdbc) != SQL_SUCCESS)
	cout << "SQLDisconnect Error" << endl;
  SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
  SQLFreeHandle(SQL_HANDLE_ENV, henv);
  cout << "End of program." << endl;
} // end main
 
Back to top
dev/odbc/examples/simpleselect.txt · Last modified: 2016/06/28 22:38 (external edit)