Esempio  di script C++ per la connessione ODBC  compilabile  con Dev-c++  e con Borland C++ builder standard

 In  Visual C++  2005  e successive  potrebbe essere necessario sostituire i SQLCHAR con SQLWCHAR la cui assegnazione  deve essere nel formato  SQLWCHAR szVar[1024] L"Testo assegnato"; per via di UNICODE e il print della variabile    usa l'operatore %ls   invece che %s .

Per la compilazione sono necessarie le librerie odbc32.lib in borland  oppure  libodbc32.a in Dev-c++

In caso di  Linker Error: Unresolved external '_SQLAllocHandle' :

Eseguire il comando nella cartella Bin di borland

implib -a odbc32.lib c:\windows\system32\odbc32.dll

sostituire tutti i vecchi  odbc32.lib dalle cartelle 

Lib/

Lib/Debug/

Lib/Release/

 con il file creato  da implib

Le librerie vanno linkate nel progetto

Project->Add to project 

 

nella sezione download  è presente il file odbc32.lib già pronto per Borland C++ Builder

 Fonte : http://www.easysoft.com/developer/languages/c/odbc_tutorial.html

 msdn Reference : https://msdn.microsoft.com/en-us/library/ms714177%28v=vs.85%29.aspx

 Reference C++:  http://www.cplusplus.com/reference/

//---------------------------------------------------------------------------
#include <cstdio>
#include <windows.h>
//#include <odbcinst.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused


int main() {
  SQLHENV env;
  SQLHDBC dbc;
  SQLHSTMT stmt;
  SQLRETURN ret; /* ODBC API return status */
  SQLCHAR outstr[1024];
  SQLSMALLINT outstrlen;
  SQLSMALLINT columns; /* number of columns in result-set */
  int row = 0;
  //int num = 0;
  UCHAR                   szDSN[SQL_MAX_DSN_LENGTH] ="DSN=dbdati;";                // Data Source Name buffer
  UCHAR*                  szUID = NULL;// User ID buffer
  UCHAR*                  szPasswd = NULL;// Password buffer
  UCHAR                   szModel[128];// Model buffer
  SDWORD             cbModel;// Model buffer bytes recieved
  UCHAR                   szSqlStr[] = "select * from tabella1;";    // SQL string
  RETCODE              retcode;// Return code


  /* Allocate an environment handle */
  SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
  /* We want ODBC 3 support */
  SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
  /* Allocate a connection handle */
  SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);

  // retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID,SQL_NTS, szPasswd, SQL_NTS);

  /* Connect to the DSN mydsn */
  ret = SQLDriverConnect(dbc, NULL, szDSN, SQL_NTS,
             outstr, sizeof(outstr), &outstrlen,
             SQL_DRIVER_COMPLETE);
  if (SQL_SUCCEEDED(ret)) {
    printf("Connected\n");
    printf("Returned connection string was:\n\t%s\n", outstr);
      /* Allocate a statement handle */
       SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
      SQLExecDirect(stmt, szSqlStr, SQL_NTS);
 
 
     //SQLTables(stmt, NULL, 0, NULL, 0, NULL, 0, "tab_utenti", SQL_NTS);
    /* How many columns are there */
    SQLNumResultCols(stmt, &columns);
    printf("Columns %d\n", columns);
    /* debug check for error  */
    ret = SQLFetch(stmt);
    printf("Fetch error %d\n", ret);
    /* Loop through the rows in the result-set */
    while (SQL_SUCCEEDED(ret = SQLFetch(stmt))) {
        SQLUSMALLINT i;
        printf("Row %d\n", row++);
        /* Loop through the columns */
        for (i = 1; i <= columns; i++) {
            SQLINTEGER indicator;
            char buf[512];
            /* retrieve column data as a string */
               ret = SQLGetData(stmt, i, SQL_C_CHAR,
                             buf, sizeof(buf), &indicator);
            if (SQL_SUCCEEDED(ret)) {
                /* Handle null columns */
                if (indicator == SQL_NULL_DATA) strcpy(buf, "NULL");
                printf("  Column %u : %s\n", i, buf);
             }
        }
    }
    SQLDisconnect(dbc);        /* disconnect from driver */
    SQLFreeHandle(SQL_HANDLE_ENV, stmt);
  } else {
    fprintf(stderr, "Failed to connect\n");
    //extract_error("SQLDriverConnect", dbc, SQL_HANDLE_DBC);
  }
 
  /* free up allocated handles */

  SQLFreeHandle(SQL_HANDLE_DBC, dbc);
  SQLFreeHandle(SQL_HANDLE_ENV, env);
 
  //scanf("%d",&num);
  getchar();
  return 0;
 
}