VC单文件连接MYSQL示例,丢掉libmysql.dll
网上很多VC连接mysql的例子都是要带个libmysql.dll才能正常使用的。
但有时我们并不想多带个libmysql.dll,只想单文件就能使用或者你是要
封装在一个dll文件里,这时你要就用到mysqlclient.lib这个静态库了。
示例代码如下,很很简单,就不多说了。
相关的mysql头文件及静态库:
http://download.csdn.net/source/1047386
#include "stdafx.h" #include <windows.h> #include <winsock2.h> #include "mysql/include/mysql.h" //忽略LIBCMT.lib, MSVCRTD.lib这些库,不然链接时会报lnk2005错误 #pragma comment(linker,"/nodefaultlib:LIBCMT.lib") #pragma comment(linker,"/nodefaultlib:MSVCRTD.lib") /*用mysqlclient.lib,不要用libmysql.lib,要不你要多带一个libmysql.dll 静态库有两个,一个是debug版的,一个是release版的,要注意 */ #if defined _DEBUG #pragma comment(lib, "mysqlclient_debug.lib") #else #pragma comment(lib, "mysqlclient.lib") #endif #pragma comment(lib, "wsock32.lib") #pragma comment(lib, "Advapi32.lib") int _tmain(int argc, _TCHAR* argv[]) { MYSQL* mysql = NULL; mysql = mysql_init(mysql); MYSQL_RES* res; MYSQL_ROW record; mysql_real_connect(mysql, "192.168.0.240", "root","root", "amx", 3306, NULL, NULL); mysql_query(mysql, "select auth, password, access from admins where length(access)>3"); res = mysql_store_result(mysql); while((record=mysql_fetch_row(res))) { printf("auth=%s,password=%s,access=%s/n", record[0], record[1], record[2]); } mysql_free_result(res); mysql_close(mysql); return 0; }