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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
//code complet
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Les defines
#define EXPORT_FILE "./test.txt"
// Les types SQL
SQLHENV henv;
SQLHDBC hdbc;
SQLHSTMT hstmt;
int main()
{
// Déclaration des variables SQL
SQLRETURN retcode;
SQLINTEGER cbville, cbnom, cbprenom;
SQLCHAR szville[81],sznom[81],szprenom[81];
// Déclaration des variables Standart
char requete[1024+1]="SELECT * FROM test WHERE ID =1 ";
char Serveur[81]="MySQL";
char Login[81]="root";
char Pwd[81]="";
SQLCHAR dsn[15] = "essais" ;
SQLCHAR uid[15] = "" ;
SQLCHAR pwd[15] = "" ;
FILE *fp = NULL;
// Connexion à la base de données
retcode=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
retcode=SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3,0);
printf("SQLAllocHandle Succesful 1 \n");
}
if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
retcode=SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
printf("SQLSetEnvAttr Succesful 2 \n");
}
else
printf("Erreur sur l'instruction SQLAllocHandle !\n\n\n");
// Vérification du Lien ODBC, Login Et Pwd
if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
retcode=SQLConnect(henv, dsn, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
else
printf("Erreur sur l'instruction SQLSetEnvAttr !\n");
// Foireux a partir de ce moment là
if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
retcode=SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
else
printf("Erreur sur l'instruction SQLConnect !\n");
// Execution de la requete
if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
retcode=SQLExecDirect(hstmt, (SQLWCHAR*)requete, SQL_NTS);
else
printf("Erreur sur l'instruction SQLAllocHandle !\n");
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
// Récupération des informations contenu dans les champs des tables
SQLBindCol(hstmt, 1, SQL_C_CHAR, szville,80, &cbville);
SQLBindCol(hstmt, 2, SQL_C_CHAR, sznom, 80, &cbnom);
SQLBindCol(hstmt, 3, SQL_C_CHAR, szprenom, 80, &cbprenom);
}
else
printf("Erreur sur l'instruction SQLExecDirect !\n\n");
// Déconnexion
SQLFreeStmt(hstmt, SQL_DROP);
SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
system("PAUSE");
return 0;
} |