- 将libmysql.dll拷贝到与代码同一目录下
- 再stdafx.h中加入#include “mysql.h”
- 将mysql安装目录下的包含文件的路径和库文件的路径在VS中进行配置
比如,点击项目–鼠标右键–属性–VC++目录–包含目录,在包含目录中加入:
C:\Program Files %28x86%29\MySQL\MySQL Server 5.6\include
在库目录中加入:
C:\Program Files (x86)\MySQL\MySQL Server 5.6\lib
退回到属性页,点击链接器–输入–附加依赖项中加入libmysql.lib; - 加入下列代码:
const char user[] = "spec3205722251";
const char pswd[] = "spec3205722251";
const char host[] = "rdsz4kl5za13u8d0m3i8spublic.mysql.rds.aliyuncs.com";
const char table[] = "liujiaoti";
unsigned int port = 3306;
mysql_init(&mysql);
if(mysql_real_connect(&mysql,host,user,pswd,table,port,NULL,0))
{
mysql_query(&mysql, "SET NAMES 'gb2312'");
initOk = true;
}
else
{
initOk = false;
}