Mysql for Visual Studio 1.1.1此文件可以用到Visual Studio2013服务器资源管理器里面添加mysql数据源,此文件还兼容VS以前版本,可以添加mysql数据源
(安装路径:D:Program FilesMySQLMySQL Server 5.5)
IDE: Microsoft Visual Studio 2010 Professional
(以上东西都是完整安装的)
-------------------------------------------
1、新建一个工程,随便写一个helloworld,目的是就是让工程下的“debug”文件夹出现
2、把D:Program FilesMySQLMySQL Server 5.5lib下面的libmysql.dll复制到工程的debug文件夹里面
3、接下来要设置一些引用文件的环境变量,首先,点击工程属性->vc++目录。
然后“include目录”那把“D:Program FilesMySQLMySQL Server 5.5include”给加进来
再然后“lib目录”那里把“D:Program FilesMySQLMySQL Server 5.5lib”也一起加进来
4、然后在工程属性窗口下点击:连接->附加依赖,把"libmysql.lib"写进去
5、源代码示范:(中文注释之间夹杂了乱码,自己跳着看吧)
#include<Windows.h>
#include<string>
#include<iostream>
#include<mysql.h>
#include<stdio.h>
intmain()
{
//connectionparams
char*host = "localhost";
char*user = "root";
char*pass = "132";//你的mysql服务器密码
char*db = "littleTest";
MYSQL*sock;
MYSQL_RES*res;
MYSQL_FIELD* fd ;
MYSQL_ROWrow;
charszSqlText[500]="";
inti;
inttcreate=0;
sock=mysql_init(0);
if(sock//初?始o?化¡¥数oy据Y结¨¢构1
&& mysql_real_connect(sock,host,user,pass,db,0,NULL,0))
{
mysql_set_character_set(sock,"utf8");
//connectioncharacter set
//waitfor possibility to check system/mysql mysqlets
if(tcreate)
{
sprintf(szSqlText, //构1造¨¬SQL语®?句?//新?建¡§一°?张?表à¨a
"create table mytable (time datetime,s1 char(6),s2 char(11),s3int,s4 int)");
if(mysql_query(sock,szSqlText))//执¡ä行Dsql语®?句?,并¡é判D断?是o?否¤?出?错ä¨a
{
cout<<"can't create table:"<<mysql_error(sock)<<endl;
mysql_close(sock);
returnFALSE;
}
}
sprintf(szSqlText,//向¨°mytable这a个?表à¨a插?入¨?数oy据Y //注á¡é意°a时o¡à间?格?式o?
"insertinto mytable values('2000-3-10', 'Test','MySQLTest',2000,15)");
if(mysql_query( sock, szSqlText))
{
cout<<"Can't insert data to table"<<mysql_error(sock)<< endl;
mysql_close( sock) ;
returnFALSE ;
}
sprintf(szSqlText, "select* from mytable");
if(mysql_query( sock, szSqlText))//进?行D数oy据Y检¨¬索¡Â,并¡é检¨¬查¨|有®D无T错ä¨a误¨®
{
mysql_close( sock );
returnFALSE ;
}
else
{
res= mysql_store_result( sock) ;//取¨?得ì?查¨|询¡¥结¨¢果?
i = (int) mysql_num_rows(res ) ;//取¨?得ì?有®D效¡ì记?录?数oy
cout<<"Query: "<<szSqlText<<" "<<i<<" records found:"<<endl;
for( i = 0 ; fd = mysql_fetch_field( res ); i++ )
cout<<fd->name<<" ";
cout<<endl;//输o?出?各¡Â字á?段?名?
while(row = mysql_fetch_row( res))//依°¨¤次ä?读¨¢取¨?各¡Â条¬?记?录?
cout<<row[0]<<" "<<row[1]<<" "<<row[2]<<" "<<row[3]<<" "<<row[4]<<endl;
mysql_free_result( res ) ;
}
}
else//连¢?接¨®数oy据Y库a出?错ä¨a
{
cout<<"cannot connect to the sock sever "<<mysql_error(sock)<<endl;
mysql_close(sock);
returnFALSE;
}
mysql_close(sock);
system("pause");
returnTRUE;
}