1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| // Connexion et execution de requete sur un base de donnees Microsoft Access
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#pragma comment(lib, "odbc32.lib")
int main(int argc, char *argv[])
{
HENV hEnv;
HDBC hDbc;
RETCODE rc;
int iOut;
char strOut[256];
char szDSN[256] = "driver={Microsoft Access Driver (*.mdb)};dbq=[test.mdb];";
char szSql[256] = "SELECT * FROM One";
int ret1;
int ret2;
int ret3;
int ID;
char Name[128];
char Login[128];
// 1 - Connexion a la BDD Access
rc = SQLAllocEnv(&hEnv);
rc = SQLAllocConnect(hEnv, &hDbc);
rc = SQLDriverConnect(hDbc, NULL, (unsigned char*)szDSN, SQL_NTS, (unsigned char*)strOut, 255, (SQLSMALLINT*)&iOut, SQL_DRIVER_NOPROMPT);
// 2 - Preparation de la requete
HSTMT hStmt;
rc = SQLAllocStmt(hDbc,&hStmt);
rc = SQLPrepare(hStmt,(unsigned char*)szSql, SQL_NTS);
// 3 - Execution de la requete (Apres avoir binder les champs de resultats)
//rc = SQLBindCol(hStmt, tab_column, tr_type, tr_value, tr_len, len_or_ind);
rc = SQLBindCol(hStmt, 1, SQL_C_ULONG, &ID, 4, (SQLINTEGER*)&ret1);
rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, Name, 128, (SQLINTEGER*)&ret2);
rc = SQLBindCol(hStmt, 3, SQL_C_CHAR, Login, 128, (SQLINTEGER*)&ret3);
rc = SQLExecute(hStmt);
// IMPORTANT : Pour les requetes du style DROP/CREATE/UPDATE...
// Vous pouvez utiliser : rc = SQLExecDirectA(hStmt,(unsigned char*)szSql,SQL_NTS);
// 4 - Boucle pour afficher les resultats
while(!(SQLFetch(hStmt) & 0xFFFE))
{
printf("{%i}{%s}{%s}\n", ID, Name, Login);
}
// 5 - Liberations et fermeture de la connection a la BDD
rc = SQLFreeStmt(hStmt, SQL_DROP);
rc = SQLDisconnect(hDbc);
rc = SQLFreeEnv(hEnv);
getch();
return 0;
} |