Connection BDD en C

cs_naru Messages postés 19 Date d'inscription samedi 7 septembre 2002 Statut Membre Dernière intervention 10 août 2004 - 4 août 2004 à 10:53
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019 - 4 août 2004 à 12:19
Bjr
J'ai un code dans lequel je dois modifier le chemin de la bdd, et rentrer les paramètres de l'utilisteur (login/pass).
Seulement, je ne vois pas très bien où... je n'ai jamais utilisé le C pour ça.
Pourriez-vous m'aider?

Voici le code :
int addStations (gdImagePtr im, float fx1,float fy1,float fx2,float fy2,int sx, int sy,int txtcolor,int nomStation)
{
float st_latitude,st_longitude;
int x,y;//,dd;
char nom[65];
HENV env; //descripteur d'environnement
HDBC dbc; //descripteur de connexion
char buf[100];//buffer pour construire la requete
HSTMT stmt;//requete SQL
RETCODE code; //code d'erreur des fonctions ODBC
SDWORD cb_lat,cb_lon,cb_nom,cb_dd;
UCHAR errmsg[100];

//allocation d'un descripteur d'environnement
SQLAllocEnv(&env);

//allocation d'un descripteur de connexion
SQLAllocConnect(env,&dbc);

//connexion à la base (SQL_NTS comme Null Terminating String)
code=SQLConnect (dbc,(UCHAR FAR *)"frienddata",SQL_NTS,(UCHAR FAR *)"sa",SQL_NTS,(UCHAR FAR *)"amani",SQL_NTS);
if(code==SQL_ERROR) {
SQLError(env,dbc,SQL_NULL_HSTMT,NULL,NULL,errmsg,sizeof(errmsg),NULL);
return 0;
}

SQLAllocStmt(dbc,&stmt);

//construction de la requete
sprintf(buf,"select st_latitude,st_longitude,st_nom from station where st_longitude between %f and %f and st_latitude between %f and %f order by st_latitude",fx1,fx2,fy1,fy2);
code=SQLExecDirect(stmt,(UCHAR FAR *) buf,SQL_NTS);
if(code==SQL_ERROR) {return 0; }
ect...

3 réponses

magic_Nono Messages postés 1878 Date d'inscription jeudi 16 octobre 2003 Statut Membre Dernière intervention 16 mars 2011
4 août 2004 à 11:53
salut à toi

un rapide coup d'oeil me dit que C po trop là...

là, ça ajoute des stations (pouvant se connecter à la base?)

Magicalement,
Magic Nono: l'informagicien!
0
cs_naru Messages postés 19 Date d'inscription samedi 7 septembre 2002 Statut Membre Dernière intervention 10 août 2004
4 août 2004 à 12:00
Si si c'est là.
En fait la fonction permet d'ajouter à l'affichage les stations trouvées dans la bdd.
Et j'ai un peu fureté dans les forums et le code correspond bien à celui pour se connecter à une bdd.
Qd aux paramétrages de connex, pour moi, il restent tjrs invisibles.

Logiquement, je les aurais placé là :
code=SQLConnect (dbc,(UCHAR FAR *)"frienddata",SQL_NTS,(UCHAR FAR *)"sa",SQL_NTS,(UCHAR FAR *)"amani",SQL_NTS)
0
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
4 août 2004 à 12:19
Vois si tu peux utiliser SQLDriverConnect si tu veux connecter sur une base precise, je fais et va impec (base Sql Server declaree 'public' dans cet exemple).

int OdbcSqlConnect(char* dbname, char* dbpwd) // dbpwd char* optionnel
{ // RETOURNE STATUS ERREUR
SQLRETURN rtcd;
char *c;
c = bnstrcpy(szbuff, "DRIVER=SQL Server;AnsiNPW=No;Trusted_Connection=Yes;AutoTranslate=No;SERVER=BIXEON\\BNSQL;DATABASE=");
c = bnstrcpy(c, dbname);
*c = ';';
if(dbpwd) {
if(dbpwd[0]) { *(c+1) 'P'; *(c+2) 'W'; *(c+3) = 'D'; *(c+4) = '='; c bnstrcpy(c+5, dbpwd); *c ';';
}
}
*(c+1) = 0;
rtcd = SQLDriverConnect(hconn, 0, (SQLCHAR*)szbuff, c - szbuff + 2,
0, 0, 0, SQL_DRIVER_NOPROMPT);
if(rtcd &= 0xFFFE) {
SQLFreeHandle(SQL_HANDLE_DBC, hconn);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
return rtcd;
}

exemple d'appel:
if(OdbcSqlConnect("MyDb", 0)) return 0;
Adapte la chaine de connexion a tes besoins.

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