Mysql for Visual Studio 1.1.1此文件可以用到Visual Studio2013服务器资源管理器里面添加mysql数据源,此文件还兼容VS以前版本,可以添加mysql数据源
连接教程:
(安装路径:D:\Program Files\MySQL\MySQL Server 5.5)
IDE: Microsoft Visual Studio 2010 Professional
(以上东西都是完整安装的)
-------------------------------------------
1、新建一个工程,随便写一个helloworld,目的是就是让工程下的“debug”文件夹出现
2、把D:\Program Files\MySQL\MySQL Server 5.5\lib下面的libmysql.dll复制到工程的debug文件夹里面
3、接下来要设置一些引用文件的环境变量,首先,点击工程属性->vc++目录。
然后“include目录”那把“D:\Program Files\MySQL\MySQL Server 5.5\include”给加进来
再然后“lib目录”那里把“D:\Program Files\MySQL\MySQL Server 5.5\lib”也一起加进来
4、然后在工程属性窗口下点击:连接->附加依赖,把"libmysql.lib"写进去
5、源代码示范:(中文注释之间夹杂了乱码,自己跳着看吧)
#include
#include
#include
#include
#include
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<
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<
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<
for( i = 0 ; fd = mysql_fetch_field( res ); i++ )
cout<name<
cout<
while(row = mysql_fetch_row( res))//依°¨¤次ä?读¨¢取¨?各¡Â条¬?记?录?
cout<
mysql_free_result( res ) ;
}
}
else//连¢?接¨®数oy据Y库a出?错ä¨a
{
cout<
mysql_close(sock);
returnFALSE;
}
mysql_close(sock);
system("pause");
returnTRUE;
}