资源描述:
《vc连接mysql的方法总结》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、#include导入一个#pragmacomment(lib,"libmysql.lib")ADO连接mysqlCoInitialize(NULL);_ConnectionPtrpConn(__uuidof(Connection));_RecordsetPtrpRst(__uuidof(Recordset));pConn->Open("Driver={MySQLODBC5.1Driver};Server=127.0.0.1;Database=test;uid=root;pwd=weiwen;","","",-1
2、);//DSN,User,PwdpRst=pConn->Execute("select*fromuser",NULL,adCmdText);while(!pRst->wenEOF){cout<<"ID:"<<(_bstr_t)pRst->GetCollect("id");cout<<"Name:"<<(_bstr_t)pRst->GetCollect("name")<MoveNext();}pRst->Close();pConn->Close();pRst.Release();pConn.Release(
3、);CoUninitialize();//ADO法访问Mysql数据库*/2.VC环境下使用mysqlAPI函数访问MySQL数据库1.安装MySQL的完整版;(在安装过程中可以选择)2.将C:ProgramFilesMySQLMySQLServer5.0libopt目录下的“libmysql.lib”文件复制到创建的VC工程目录下;3.程序中包括头文件,连入库文件:#include#include"C:ProgramFilesMySQLMySQLServer5.0includemysq
4、l.h"(数据库的安装路径)#pragmacomment(lib,"libmysql.lib")4.程序例子#include"stdafx.h"#include#include"C:ProgramFilesMySQLMySQLServer5.0includemysql.h"#pragmacomment(lib,"libmysql.lib")intmain(intargc,char*argv[]){MYSQLmysql;MYSQL_RES*result;MYSQL_ROWrow;mysql_init
5、(&mysql);//初始化mysql结构if(!mysql_real_connect(&mysql,"MyXp","UserDB","123456","userdata",0,NULL,0))//连接服务器{fprintf(stderr,"Failedtoconnecttodatabase:Error:%s",mysql_error(&mysql));}//////////////////////////////////////////////////////////////////////////////////////
6、/////////////插入数据/////////////////////////////////////////if(mysql_query(&mysql,"insertintoUserDataTablevalues('rm','209','209','2153@qq.com','23213','yjiang','26.222.171.2','2010-04-0110:53:12')"))//进行查询{fprintf(stdout,"mysql_query()insertfailed!");}//////////////
7、///////////////////////////////////////////////////////////////////////////////////////////查询数据///////////////////////////////if(mysql_query(&mysql,"select*fromUserDataTable")){fprintf(stdout,"mysql_query()selectfailed!");}result=mysql_store_result(&mysql);//读取一个到客
8、户的查询的全部结果,//分配一个MYSQL_RES结构,并且把结果放进这个结构中。(获得结果集)while(row=mysql_fetch_row(result))//取出结果集中记录{fprintf(stdout,"UserName:%spassword:%s