A mon avis, c'est pas avec des bidules classes que tu apprendras grand chose d'ODBC, surtout que c'est assez simple en prenant un peu l'habitude.
Regarde exemple dessous, tu obtiens un exe de 3 Ko et sans aucune dépendance, tu files l'exe et il tourne sur tout Windows 32 bits.
Exemple d'emploi ODBC
On va insérer 50 enregs dans une table Sql Server, instance nommée "Toto".
// Base "Test"
// TABLE "histos"
// Fld(0) : histID, INT32 AutoIncr
// Fld(1) : histDat, datetime(8)
// Fld(2) : histSit, char(4)
// Fld(3) : Qte, INT32
J'insère le champ histSit en mode binaire au lieu de text, évite au pilote de calculer sa longueur avant insertion.
J'incrémente la date et la quantité de 1 à chaque tour de boucle.
--------------------------------
#define _WIN32_WINNT 0x0600
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#pragma comment(lib, "odbc32.lib")
char szappname[] = "Histo";
char szCONN[] = "DRIVER=SQL Server;AnsiNPW=No;Trusted_Connection=Yes;AutoTranslate=No;SERVER=Bnx64\\Toto;DATABASE=Test";
SQLHANDLE henv, hconn;
int __stdcall OdbcConnEnvCreate()
{
if(SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv) & 0xFFFE) goto errMsg;
if(SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,0) & 0xFFFE) goto envOut;
if(!(SQLAllocHandle(SQL_HANDLE_DBC, henv, &hconn) & 0xFFFE)) return 0;
envOut:
SQLFreeHandle(SQL_HANDLE_ENV, henv);
errMsg:
return MessageBox(0, "ODBC INDISPONILE", szappname, 0x30);
}
int __stdcall OdbcSqlConnect()
{ // RETOURNE STATUS ERREUR
SQLRETURN rtcd;
rtcd = SQLDriverConnect(hconn, 0, szCONN, 104 /*strlen(szCONN)*/, 0, 0, 0, SQL_DRIVER_NOPROMPT);
return (rtcd & 0xFFFE);
}
void __stdcall testInsert()
{
SQLHANDLE hstmt;
char szsite[4];
int dat, qte;
int rdat = 0, rsit = 4, rqte = 0;
int i;
if(SQLAllocHandle(SQL_HANDLE_STMT, hconn, &hstmt) & 0xFFFE) return;
if(SQLPrepare(hstmt, "INSERT INTO histos (histDat,histSit,Qte) VALUES (?,?,?)", SQL_NTS) & 0xFFFE) goto freeSTMT;
if(SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_INTEGER, SQL_INTEGER, 0, 0, &dat, 0, &rdat) & 0xFFFE) goto freeSTMT;
if(SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_BINARY, SQL_BINARY, 4, 0, szsite, 0, &rsit) & 0xFFFE) goto freeSTMT;
if(SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT, SQL_INTEGER, SQL_INTEGER, 0, 0, &qte, 0, &rqte) & 0xFFFE) goto freeSTMT;
*((DWORD*) szsite) = 'ESUR';
dat = 39160; // 39159 = 20/03/2007 - 2
qte = 200;
i = 50;
do {
if(SQLExecute(hstmt) & 0xFFFE) goto freeSTMT;
dat++;
qte++;
} while(--i);
MessageBox(0, "OK", szappname, 0);
freeSTMT: SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
}
#pragma comment(linker, "/entry:myWinMain")
__declspec(naked) void __stdcall myWinMain()
{
__asm {
call OdbcConnEnvCreate
test eax, eax
jne short progEND
call OdbcSqlConnect
test eax, eax
jne short relHANDLES
call testInsert
closeCONN:
push hconn
call SQLDisconnect
relHANDLES:
push hconn
push SQL_HANDLE_DBC
call dword ptr SQLFreeHandle
push henv
push SQL_HANDLE_ENV
call dword ptr SQLFreeHandle
progEND:
push 0
call dword ptr ExitProcess
}
}
ciao...
BruNews, MVP VC++