First install MySQL, vs2008
Add the following code to the. cpp file of the VC ++ project:
# Include <winsock2.h> // define socket # include "MySQL. H "// # pragma comment (Lib," libmysql. lib "); // Add libmysql to the attached dependency. lib functions are the same
The following configuration in project properties: 1. Add the directory containing the header file mysql. H, include is the mysql-5.0.27-win32 \ include folder. 2, additional library directory, MySQL lib file is mysql-5.0.27-win32 \ Lib \ opt file 3, additional dependencies, name libmysql. lib4: Convert libmysql. DLL copy to the debug folder, libmysql. DLL in the Lib folder
Define global SQL
MYSQL mysql;
Mysql_init (& MySQL); // initialize bool isconnected = mysql_real_connect (& MySQL, "localhost", "root", "131015", "firstsqldb", 3306, 0, 0 ); // Connect if (isconnected) {MessageBox (_ T ("Success connected! "); Return;} else {int I = mysql_errno (& MySQL); // connection error const char * s = mysql_error (& MySQL ); messageBox (_ T ("failed to connect! Please check for your DB service. ");} mysql_close (& MySQL); // close sqlmysql_res * result; mysql_row SQL _row; mysql_field * FD; int res = mysql_query (& MySQL, "select * From mytable"); // execute the SQL command if (! Res) {result = mysql_store_result (& MySQL); // Save the queried data to result if (result) {int I, j; for (I = 0; FD = mysql_fetch_field (result); I ++) // obtain the column name {STR [I]. format ("S", FD-> name); forshow = forshow + STR [I] + "\ t";} J = mysql_num_fields (result ); while (SQL _row = mysql_fetch_row (result) // obtain the specific data {for (I = 0; I <j; I ++) ss. format ("S", SQL _row [I]);} If (result! = NULL) mysql_free_result (result) ;}} else {MessageBox (_ T ("query SQL failed! "));}