Ecrire dans une base de données access grâce à ODBC

volcomboy1 Messages postés 3 Date d'inscription mardi 28 janvier 2003 Statut Membre Dernière intervention 19 février 2003 - 11 févr. 2003 à 16:02
sarl_adc Messages postés 141 Date d'inscription lundi 3 novembre 2003 Statut Membre Dernière intervention 20 octobre 2005 - 8 juin 2004 à 15:20
Bonjour
Pourriez vous m'expliquer comment on fait pour créer des nouveau champs et des nouveau enregistrements dans une base de données acces grâce a odbc sous visual c++. Merci

4 réponses

BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
11 févr. 2003 à 20:15
voici un exemple en 1 seul fichier.

#include <windows.h>
#include <sql.h>
#include <sqlext.h>

#pragma comment(lib, "odbc32.lib")

HINSTANCE hinst;
SQLHANDLE henv, hconn;
char szbuff[1024];
char *szappname = "Requete";

inline char* bnstrcpy(char *dst, char *src)
{ // return ptr sur NULL final
__asm {
mov eax, dst
mov ecx, src
dec eax
LcpyLoop:
mov dl, [ecx]
inc eax
inc ecx
mov [eax], dl
or dl, dl
jz short LcpyOut
mov dl, [ecx]
inc eax
inc ecx
mov [eax], dl
or dl, dl
jz short LcpyOut
mov dl, [ecx]
inc eax
inc ecx
mov [eax], dl
or dl, dl
jnz short LcpyLoop
LcpyOut:
}
}

int OdbcConnEnvCreate()
{
SQLRETURN retcd;
retcd = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
if(retcd == SQL_SUCCESS) goto envOK;
if(retcd == SQL_SUCCESS_WITH_INFO) goto envOK;
strcpy(szbuff, "Environment handle allocation failed");
goto errMsg;
envOK:
retcd = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3,0);
if(retcd == SQL_SUCCESS) goto versOK;
if(retcd == SQL_SUCCESS_WITH_INFO) goto versOK;
strcpy(szbuff, "ODBC version incorrecte");
goto envOut;
versOK:
retcd = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hconn); if((retcd SQL_SUCCESS) || (retcd SQL_SUCCESS_WITH_INFO)) return 1;
strcpy(szbuff, "Connection handle allocation failed");
envOut:
SQLFreeHandle(SQL_HANDLE_ENV, henv);
errMsg:
MessageBox(0, szbuff, szappname, MB_ICONERROR);
return 0;
}

int OdbcMdbConnect(int bReadOnly, char* dbname, char* dbpwd)
{ // dbname et dbpwd en options
// dbpwd pris en compte SI dbname est fourni
SQLRETURN retcd;
if(bReadOnly)
SQLSetConnectAttr(hconn, SQL_ATTR_ACCESS_MODE, (SQLPOINTER)SQL_MODE_READ_ONLY, 0);
char *c = bnstrcpy(szbuff, "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=");
if(dbname) {
if(dbname[0]) {
c = bnstrcpy(c, dbname);
if(dbpwd) {
if(dbpwd[0]) { *c ';'; *(c+1) 'P'; *(c+2) = 'W'; *(c+3) = 'D'; *(c+4) = '=';
c = bnstrcpy(c+5, dbpwd);
}
}
}
}
retcd = SQLDriverConnect(hconn, 0, (SQLCHAR*)szbuff, c - szbuff,
0, 0, 0, SQL_DRIVER_COMPLETE_REQUIRED); if((retcd SQL_SUCCESS) || (retcd SQL_SUCCESS_WITH_INFO)) return 1;
SQLFreeHandle(SQL_HANDLE_DBC, hconn);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
return 0;
}

int OdbcExecAction(char *pszqry)
{ // return != 0 SI ERREUR
SQLHANDLE hstmt;
SQLRETURN retcd;
retcd = SQLAllocHandle(SQL_HANDLE_STMT, hconn, &hstmt);
if(retcd & 0xFFFE) return 1;
retcd = SQLExecDirect(hstmt, (SQLCHAR*)pszqry, 60);
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
return (retcd & 0xFFFE);
}

void AppPathInitialize()
{
char szThis[300];
char* c = szThis + GetModuleFileName(NULL, szThis, 300); while(*c !'\\') c--; *c 0;
SetCurrentDirectory(szThis);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, int)
{
hinst = hInstance;
AppPathInitialize();
if(!OdbcConnEnvCreate()) return 0;
if(!OdbcMdbConnect(0, "Mybase.mdb", 0)) return 0;
MessageBox(0, "OUVERT", szappname, MB_ICONINFORMATION);
//if(!OdbcExecAction("CREATE TABLE T7 (a INTEGER, b INTEGER, c TIMESTAMP)"))
// MessageBox(0, "QUERY OK", szappname, MB_ICONINFORMATION);
if(!OdbcExecAction("SELECT * INTO T2 IN Tmp.mdb FROM T1 WHERE a < 10"))
MessageBox(0, "QUERY OK", szappname, MB_ICONINFORMATION);
SQLDisconnect(hconn);
SQLFreeHandle(SQL_HANDLE_DBC, hconn);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
return 0;
}

ciao...
0
cs_yAAm Messages postés 45 Date d'inscription samedi 31 mai 2003 Statut Membre Dernière intervention 22 février 2006
27 mai 2004 à 10:31
Bonjour Mr bruNeuwws à qui j'ai deja eu à faire pour ce genre de sujet, mais quelque chose me titille les synapses : le "c" n'est pas un tableau, mais tu ecrit derriere le "premier element" pointé par c ?/!! comprend po
0
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
27 mai 2004 à 10:37
Je ne sais pas de quelle ligne tu parles mais en general j'emploie 'c' en pointeur char* donc je ne vois pas ou serait le probleme.

ciao...
BruNews, Admin CS, MVP Visual C++
0
sarl_adc Messages postés 141 Date d'inscription lundi 3 novembre 2003 Statut Membre Dernière intervention 20 octobre 2005
8 juin 2004 à 15:20
Hello,

J'ai testé ce code et il fonctionne pas mal du tout !

En revanche, j'avais une question-complément à poser :
Je voudrais ajouter un enregistrement dans une base en "Pervasive SQL 2000 (SP3)" mais avec ce code je n'y arrive pas.

Je ne m'y connais pas vraiment en gestion de bdd en C++ (Je n'y touche qu'en VB) et je galère pas mal.
Pour vulgariser, je peux dire que je n'ai pas de ligne de type :
"DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" dans ma chaine de connexion avec Pervasive, mais :
"Provider=MSDASQL.1;Persist Security Info=False;Data Source=base_test".

Comment je peux intégrer cela dans ce code ? J'ai tout essayé et il m'est impossible de faire quoi que ce soit, il refuse tout simplement de se connecter...

Merci par avance,
0
Rejoignez-nous