Re: Connecter au Mysql via ODBC à partir d' un programme C sous Visual Studio 2008 Wind ows
Martin Evans <[email protected]> Mon, 13 Jul 2009 10:06:23 +0100
| Newsgroups | gmane.comp.db.mysql.odbc |
|---|---|
| Organization | Easysoft Limited |
| Message-ID | <[email protected]> |
Mario Fernando RodrÃguez wrote:
> Hi, I hope my french-english translation is coherent enough, but what
> Rim msedi said was something like:
>
> "
> Hello,
>
> I want to connect a MySQL database via ODBC fron a C program in Visual
> Studio 2008 Windows.
>
> I have Mysql version 4.0.15 (esayphp 1.7)
> and mysql-connector-odbc-3.51.19-win32
>
> I tested the data source (ODBC)that I've created with the database and
> it connects normally.
>
> the DSN name is : toto
> the username : root
> the password: vide
>
>
> Here's a part of my C program:
> #include <windows.h>
> #include <stdio.h>
> #include <conio.h>
> #include<stdlib.h>
> #include<string.h>
>
> #include <sql.h>
> #include <sqlext.h>
> #include <sqltypes.h>
>
> #include <sqlucode.h>
> #include <odbcinst.h>
> #include <Msdasql.h>
> #include <Msdadc.h>
>
>
> void main()
> {
> SQLRETURN retcode;
> //char design[20];
> int A,B,C,D;
> char request[100];
> //char dsn[20]="toto";
> //char uid[20]="root";
> //char pwd[20]="";
> SQLINTEGER lon,la,lb,lc,ld,taille=SQL_NTS;
> SQLCHAR design[20];
> SQLHDBC hdbc;
> SQLHENV henv;
> SQLHSTMT hstmt;
>
>
> /*Allocate environment handle */
> retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
>
> if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) //{
> /* Set the ODBC version environment attribute */
> retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3,
> 0);
>
> if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) //{
> /* Allocate connection handle */
> retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
>
> // if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
>
> //printf("%s","vrai");
> //else
> //printf("erreur\n");
> // }
>
> //}
>
>
> // retcode = SQLConnect(hdbc,(SQLCHAR*)dsn, SQL_NTS,(SQLCHAR*)uid,
> SQL_NTS,(SQLCHAR*)pwd, SQL_NTS);
> retcode=SQLConnect(hdbc,(SQLCHAR *)"toto",SQL_NTS,(SQLCHAR
> *)"root",SQL_NTS,(SQLCHAR *)"",SQL_NTS);
> if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
> printf("%s","vrai");
> else
> printf("Erreur sur l'instruction SQLConnect !\\n");
>
>
> SQLAllocStmt ( hdbc, &hstmt);
> SQLBindCol(hstmt,1,SQL_C_CHAR,design,sizeof(design),&lon);
> SQLBindCol(hstmt,2,SQL_C_ULONG,&A,0,&la);
> SQLBindCol(hstmt,3,SQL_C_ULONG,&B,0,&lb);
> SQLBindCol(hstmt,4,SQL_C_ULONG,&C,0,&lc);
> SQLBindCol(hstmt,5,SQL_C_ULONG,&D,0,&ld);
> //SQLExecDirect(hstmt,(UCHAR FAR *)"select * from connaissance",SQL_NTS);
> strcpy(request,"select * from ");
> strcat(request,nom_table);
> strcat(request,";");
> //strcat(request," where designation like ?");
> //SQLPrepare(hstmt,"select * from connaidiabete where designation like
> ?",SQL_NTS);
> SQLExecDirect(hstmt,(UCHAR FAR *)request,SQL_NTS);
> //SQLPrepare(hstmt,request."where designation like ?",SQL_NTS);
> //SQLBindParameter(hstmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,
> SQL_VARCHAR,20,0,valeur,0,&taille);
> //SQLExecute(hstmt);
>
> while (SQLFetch(hstmt)!=SQL_NO_DATA)
> {
>
> if(strcmp(valeur,design)==0)
> printf("oui\n");
>
> }
> SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
> SQLFreeHandle(SQL_HANDLE_ENV,henv);
> SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
> SQLDisconnect(hdbc);
>
> //SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
> //SQLFreeHandle(SQL_HANDLE_ENV,henv);
> }
>
>
> After the compilation and execution I get the following message:
> Error in the SQLConnect instruction !\n
>
> Meaning the connection does not go through.
>
> I've consulted the documentation about the connection string on visual
> studio 2008 on the following link. But I did not find a solution.
>
> http://msdn.microsoft.com/en-us/library/ms711810(VS.85).aspx
>
> How can this problem be resolved?
> Have you a testing example for trying my conection and Avez-vous un
> exemple de test pour tester ma connexion et try some queries?
>
> The rest of work is based on the results of the connection to the
> database and the execution of queries. Le reste de travail se base sur
> la réussite du connexion au base des
>
> Please I need your help.
>
> A++
> "
>
> Now, the rest is up to the C - MySQL gurus ... (my field is not C ...
> sorry about that!)
>
> Cheers.
>
> Mario F. RodrÃguez
> Highland Systems Xela
> Quetzaltenango, Guatemala
>
I suggest you call SQLError or preferably, SQLGetDiagRec (as you are
asking for ODBC 3) after the connect call fails to find out what is
wrong with your connect call.
Also, you can use the SQL_SUCCEEDED macro instead of always checking for
SQL_SUCCESS and SQL_SUCCESS_WITH_INFO.
Martin
--
Martin J. Evans
Easysoft Limited
http://www.easysoft.com
--
MySQL ODBC Mailing List
For list archives: http://lists.mysql.com/myodbc
To unsubscribe: http://lists.mysql.com/[email protected]