Configuration ODBC

Résolu
picinounours51 Messages postés 23 Date d'inscription mercredi 7 mai 2003 Statut Membre Dernière intervention 23 août 2005 - 17 août 2004 à 10:23
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019 - 17 août 2004 à 11:19
Bonjour

je voudrais savoir comment configuré l'ODBC pour pouvoir ajouter une nouvelle Data Source.
J'ai essayé "SQLConfigDataSource" mais je n'arrive pas à configuré correctement les attributs.

SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)","DNS=BaseDeDonnée\0" "DataDirectory=D:\\Data\0");

Merci d'avance à tous ceux qui pourront m'apporter leur aide.

3 réponses

BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
17 août 2004 à 10:55
Te connecter direct a ta base mdb n'irait pas aussi bien ?

ciao...
BruNews, Admin CS, MVP Visual C++
3
picinounours51 Messages postés 23 Date d'inscription mercredi 7 mai 2003 Statut Membre Dernière intervention 23 août 2005
17 août 2004 à 11:11
En fait je ne sais pas comment me connecter directement.
Donc si tu sais comment faire pour, cela me serait d'une grande aide.

Merci d'avance
3
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
17 août 2004 à 11:19
Un exemple, si prob tu demandes mais regarde la doc MSDN avant.

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

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

HINSTANCE hinst;
SQLHANDLE henv, hconn;
long lerror = 0;

char szbuff[1024];
char *szappname = "Connect";

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 unic, int rdonly, char* dbname, char* dbpwd) // les 2 char* options
{
SQLRETURN retcd;
retcd = SQLSetConnectAttr(hconn, SQL_ATTR_ACCESS_MODE, (SQLPOINTER)SQL_MODE_READ_ONLY, 0);
char *c = bnstrcpy(szbuff, "DRIVER={Microsoft Access Driver (*.mdb)};");
if(unic) c = bnstrcpy(c, "EXCLUSIVE=1;"); *c 'D'; *(c+1) 'B'; *(c+2) = 'Q'; *(c+3) = '='; *(c+4) = 0;
if(rdonly) SQLSetConnectAttr(hconn, SQL_ATTR_ACCESS_MODE, (SQLPOINTER)SQL_MODE_READ_ONLY, 0);
if(dbname) {
c += 4;
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;
}

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(1 , 0, "Mybase.mdb", 0)) return 0;

MessageBox(0, "OK", szappname, MB_ICONINFORMATION);
SQLDisconnect(hconn);
SQLFreeHandle(SQL_HANDLE_DBC, hconn);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
return 0;
}

ciao...
BruNews, Admin CS, MVP Visual C++
3
Rejoignez-nous