L’esempio si basa su una sorgente dati chiamata “esempioODBC”
Il DB relativo e’ realizzato in Access e contiene una sola tabella “studenti” con matricola (integer), cognome (varchar) e nome (varchar).
Buon Lavoro !
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#define MAX_DATA 100
void visualizza(HSTMT hstmt);
void main() {
HENV henv;
// Environment handle
HDBC hdbc;
// Connection handle
HSTMT hstmt;
// Statement handle
RETCODE rc;
// Return code for ODBC functions
/*ALLOCAZIONE E CONNESSIONE*/
SQLAllocEnv(&henv);
SQLAllocConnect(henv, &hdbc);
rc = SQLConnect(hdbc, (unsigned char *)
"esempioODBC", SQL_NTS,
NULL, SQL_NTS /*nessuna password*/);
SQLAllocStmt(hdbc, &hstmt);
if ((rc != SQL_SUCCESS) && (rc !=
SQL_SUCCESS_WITH_INFO)) {
printf("Connessione non riuscita!\n");
exit(0);
}
printf("Connessione
riuscita!\n");
rc=SQLExecDirect(hstmt, (unsigned char *) "select
* from studenti",
SQL_NTS);
if ((rc != SQL_SUCCESS) && (rc
!= SQL_SUCCESS_WITH_INFO)) {
/*Qui va il codice per la gestione dell'errore*/
printf("Query non riuscita!\n");
exit(0);
}
printf("Query eseguita correttamente!\n");
short int cols;
/*Numero di colonne nel
risultato*/
rc = SQLNumResultCols(hstmt,&cols);
if ((rc != SQL_SUCCESS) && (rc !=
SQL_SUCCESS_WITH_INFO)) {
printf("Impossibile estrarre il numero di
colonne!\n");
exit(0);
}
printf("%d colonne nel risultato\n",cols);
visualizza(hstmt);
/*Query preparata*/
/*rilasciamo e riallochiamo lo Statement handle*/
SQLFreeStmt(hstmt, SQL_DROP);
SQLAllocStmt(hdbc, &hstmt);
rc = SQLPrepare(hstmt, (unsigned char *)
"select * from studenti where matricola = ?",
SQL_NTS);
if ((rc != SQL_SUCCESS) && (rc
!= SQL_SUCCESS_WITH_INFO)) {
printf("Impossibile preparare la query!\n");
exit(0);
}
printf("Query preparata\n");
/*Bind dell'unico parametro*/
int matr;
SQLBindParameter(hstmt, 1,
SQL_PARAM_INPUT, SQL_C_SHORT,
SQL_INTEGER,
0, 0, &matr, 0, NULL);
matr = 1;
rc = SQLExecute(hstmt);
if ((rc != SQL_SUCCESS) && (rc
!= SQL_SUCCESS_WITH_INFO)) {
printf("Impossibile eseguire la query!\n");
exit(0);
} else visualizza(hstmt);
/*Rilasciamo i risultati
precedenti ed eseguiamo di nuovo la query*/
SQLCloseCursor(hstmt);
matr = 2;
rc = SQLExecute(hstmt);
if ((rc != SQL_SUCCESS) && (rc
!= SQL_SUCCESS_WITH_INFO)) {
printf("Impossibile eseguire la query!\n");
exit(0);
} else visualizza(hstmt);
/*Rilasciamo i risultati
precedenti ed eseguiamo di nuovo la query*/
SQLCloseCursor(hstmt);
matr = 0;
rc = SQLExecute(hstmt);
if ((rc != SQL_SUCCESS) && (rc
!= SQL_SUCCESS_WITH_INFO)) {
printf("Impossibile eseguire la query!\n");
exit(0);
} else visualizza(hstmt);
/*Rilascio risorse e disconnessione*/
SQLFreeStmt(hstmt, SQL_DROP);
SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);
}
void visualizza(HSTMT hstmt)
{
/*Visulizzazioe del risultato*/
SDWORD cbData; // Output
length of data
RETCODE rc1, rc2;
char *szData;
szData = new char[MAX_DATA];
rc1 = SQLFetch(hstmt);
printf("\nRisultato della query\n");
while (rc1 == SQL_SUCCESS) {
int
i=1;
while ((rc2 = SQLGetData(hstmt, i, SQL_CHAR, szData,
MAX_DATA - 1,&cbData)) == SQL_SUCCESS) {
if (cbData > 0) printf("%s\t", szData);
else printf("NULL\t", szData);
i++;
}
rc1
= SQLFetch(hstmt);
printf("\n");
}
printf("\n");
}