Mysql for Visual Studio 1.1.1此文件可以用到Visual Studio2013服務(wù)器資源管理器里面添加mysql數(shù)據(jù)源,此文件還兼容VS以前版本,可以添加mysql數(shù)據(jù)源
連接教程:
(安裝路徑:D:\Program Files\MySQL\MySQL Server 5.5)
IDE: Microsoft Visual Studio 2010 Professional
(以上東西都是完整安裝的)
-------------------------------------------
1、新建一個(gè)工程,隨便寫一個(gè)helloworld,目的是就是讓工程下的“debug”文件夾出現(xiàn)
2、把D:\Program Files\MySQL\MySQL Server 5.5\lib下面的libmysql.dll復(fù)制到工程的debug文件夾里面
3、接下來要設(shè)置一些引用文件的環(huán)境變量,首先,點(diǎn)擊工程屬性->vc++目錄。
然后“include目錄”那把“D:\Program Files\MySQL\MySQL Server 5.5\include”給加進(jìn)來
再然后“l(fā)ib目錄”那里把“D:\Program Files\MySQL\MySQL Server 5.5\lib”也一起加進(jìn)來
4、然后在工程屬性窗口下點(diǎn)擊:連接->附加依賴,把"libmysql.lib"寫進(jìn)去
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服務(wù)器密碼
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(jì)?化¡¥數(shù)oy據(jù)Y結(jié)¨¢構(gòu)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, //構(gòu)1造¨¬SQL語®?句?//新?建¡§一°?張?表à¨a
"create table mytable (time datetime,s1 char(6),s2 char(11),s3int,s4 int)");
if(mysql_query(sock,szSqlText))//執(zhí)¡ä行Dsql語®?句?,并¡é判D斷?是o?否¤?出?錯(cuò)ä¨a
{
cout<<"can't create table:"<<mysql_error(sock)<<endl;
mysql_close(sock);
returnFALSE;
}
}
sprintf(szSqlText,//向¨°mytable這a個(gè)?表à¨a插?入¨?數(shù)oy據(jù)Y //注á¡é意°a時(shí)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))//進(jìn)?行D數(shù)oy據(jù)Y檢¨¬索¡Â,并¡é檢¨¬查¨|有®D無T錯(cuò)ä¨a誤¨®
{
mysql_close( sock );
returnFALSE ;
}
else
{
res= mysql_store_result( sock) ;//取¨?得ì?查¨|詢¡¥結(jié)¨¢果?
i = (int) mysql_num_rows(res ) ;//取¨?得ì?有®D效¡ì記?錄?數(shù)oy
cout<<"Query: "<<szSqlText<<"\n"<<i<<" records found:"<<endl;
for( i = 0 ; fd = mysql_fetch_field( res ); i++ )
cout<<fd->name<<"\t";
cout<<endl;//輸o?出?各¡Â字á?段?名?
while(row = mysql_fetch_row( res))//依°¨¤次ä?讀¨¢取¨?各¡Â條¬?記?錄?
cout<<row[0]<<"\t"<<row[1]<<"\t"<<row[2]<<"\t"<<row[3]<<"\t"<<row[4]<<endl;
mysql_free_result( res ) ;
}
}
else//連¢?接¨®數(shù)oy據(jù)Y庫a出?錯(cuò)ä¨a
{
cout<<"cannot connect to the sock sever "<<mysql_error(sock)<<endl;
mysql_close(sock);
returnFALSE;
}
mysql_close(sock);
system("pause");
returnTRUE;
}