This is an old revision of the document!


DRAFT

ODBC Simple SELECT

  • SQLConnect can only use System and Machine datasources
  • 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
#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;
  SQLRETURN retcode;
  char errormsg[255] = "x";
  SQLCHAR * OutConnStr = (SQLCHAR * )malloc(255);
  SQLSMALLINT * OutConnStrLen = (SQLSMALLINT *)malloc(255);
 
  cout << "OdxODBCConnectionTest" << endl;
  // Allocate environment handle
  retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
    cout << "Alloc ENV retcode:" << retcode << endl;
  retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC2, 0); 
    cout << "Set Env retcode: " << retcode << endl;
  retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc); 
    cout << "Alloc hdbc retcode: " << retcode << endl;
  retcode = SQLDriverConnect(hdbc, NULL,
	     (SQLCHAR*) "FILEDSN=c:\\dev\\odx\\tiny\\tiny.dsn", SQL_NTS,
		 (SQLCHAR*) NULL, 0, NULL, 0);
  cout << "Connect retcode:" << retcode << endl;
  if (retcode != SQL_SUCCESS)
  {
    retcode = SQLError(henv, hdbc, NULL, NULL , NULL, (SQLCHAR*) errormsg, sizeof(errormsg), NULL);
      cout  << errormsg << endl;
  }
  retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); 
  cout << "Alloc stmt:" << retcode << endl;
  retcode = SQLExecDirect(hstmt, (SQLCHAR*) "select mytext from table1 where mytext='bears'", SQL_NTS);
  cout << "Exec Direct retcode:" << retcode << endl;
  SQLCHAR mytext[60];
  retcode = SQLBindCol(hstmt, 1, SQL_C_CHAR, (SQLPOINTER) &mytext,
    sizeof(mytext), NULL);
  while (retcode==0)
  {
  retcode = SQLFetch(hstmt);
  cout << mytext << endl;
  }
  SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
  retcode = SQLDisconnect(hdbc);
  SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
  SQLFreeHandle(SQL_HANDLE_ENV, henv);
  cout << "End of program." << endl;
} // end main
 
Back to top
dev/odbc/examples/simpleselect.1260068585.txt.gz ยท Last modified: 2016/06/28 22:38 (external edit)