868k 868k - 1 year ago 293
MySQL Question

Connecting MySQL with C++/ C# in Visual Studio

I know C++ and (MySQL with PHP). But now I need to connect MySQL with C++. I need help for MySQL server , MySQL Connector settings and configuration of Code::Blocks as well as connection code. Please help me.
I know only Code::blocks or Visual Studio only. Any thing about MySQL server and Connector connector I don't know for C++ connection. Please help me.

Answer Source

For Visual Studio 2010 setup the following :

  • MySQL SERVER 5.5.38 ( mysql-5.5.38-win32.msi )
  • MySQL Connector Net 6.8.3 ( mysql-connector-net-6.8.3.msi )

In Visual Studio =>> File =>> New =>> Project =>> Visual C++ =>> CLR =>> CLR Empty Project

enter image description here

After opening the project, View =>> Solution Explorer =>> On Project Name Right Click =>> Properties =>> C/C++ Or Configuration Properties =>> General =>> Additional Include Directories =>> Click to edit and write:

C:\Program Files\MySQL\include (or that directory where you had installed MySQL and Mention its include folder).

E.g. ( C:\Program Files\MySQL\MySQL Server 5.5\include )

enter image description here

Now in same properties dialog navigate to Linker =>> General =>> "Add additional Library Directories" =>> (write the path of folder which contains .dll or .lib files e.g. C:\Program Files\MySQL\Connector ODBC)

E.g. ( C:\Program Files\MySQL\MySQL Connector Net 6.3.0 )

enter image description here

In same properties dialog navigate to Linker =>> Input =>> Additional Dependencies =>> (write the name of .lib file you want to use e.g. libmysql.lib or any other library you want to use which is placed in above "Additional Library Directories"). E.g. ( libmysql.lib )

enter image description here

And at last if it not work then please go to C:\Program Files\MySQL\include (or that directory where you had installed MySQL and Mention its include folder).

Then in Windows Search option search write "libmysql.lib". Then copy this lib file in your project bin debug,release folder. Also some times it may say in error message for requirements of some other lib or DLL file.

Do same process search in MySQL directory and copy-paste them. If this not work then copy the above mentioned DLL, lib file in each folder of your project then in it must work. I did in this way.

MySQL code for connection in Database:

#include "my_global.h"
#include "mysql.h" 
#define SERVER "localhost"
#define USER "root"
#define PASSWORD ""
#define DATABASE "database" 
int main()
    char ch;
    clock_t begin, end;
    //MYSQL *connect=mysql_init(NULL); 
    //if( ! connect) { printf("MySQL Initialization or Connection Failed!\n"); return 0; }
    //mysql_query(connect,"insert into test values(2)");

    //mysql_query(connect,"INSERT INTO student VALUES('111','aaaa','bbbb')");
    begin = clock();
    //printf("Application SN\tMerit\tAdmission Roll\n-------------- ------- ----------------\n");
    MYSQL_RES *res_set; MYSQL_ROW row; 
    mysql_query(connect,"SELECT * FROM database");
    res_set = mysql_store_result(connect);  //unsigned int numrows = mysql_num_rows(res_set);   
   // while ( row = mysql_fetch_row(res_set) )
       //printf("  res  %s\t",row[0]); /* Print the row data */ 
    end = clock();
    printf("Execution Time: %f seconds\n", (double)(end - begin) / CLOCKS_PER_SEC);
    return 0;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download