源代码如下:

// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>
using namespace std;

#define OTL_ODBC_MYSQL
#define OTL_UNICODE

#pragma comment(lib,"myodbc3.lib")

#include "otlv4.h"

otl_connect db;

int _tmain(int argc, _TCHAR* argv[])
{
char* constr="Driver={MySQL ODBC 3.51 Driver};Server=localhost; Database=test; Uid=root; Pwd=root; CharSet=UTF8; ";
otl_connect::otl_initialize();
try
{
cerr<<constr<<endl;
db.rlogon(constr);
cerr<<"已链接数据库"<<endl;
db.logoff();
}
catch(otl_exception& p)
{
cerr<<p.msg<<endl;
cerr<<p.stm_text<<endl;
cerr<<p.code<<endl;
cerr<<p.sqlstate<<endl;
cerr<<p.var_info<<endl;
}

return 0;
}// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>
using namespace std;

#define OTL_ODBC_MYSQL
#define OTL_UNICODE

#pragma comment(lib,"myodbc3.lib")

#include "otlv4.h"

otl_connect db;

int _tmain(int argc, _TCHAR* argv[])
{
char* constr="Driver={MySQL ODBC 3.51 Driver};Server=localhost; Database=test; Uid=root; Pwd=root; CharSet=UTF8; ";
otl_connect::otl_initialize();
try
{
cerr<<constr<<endl;
db.rlogon(constr);
cerr<<"已链接数据库"<<endl;
db.logoff();
}
catch(otl_exception& p)
{
cerr<<p.msg<<endl;
cerr<<p.stm_text<<endl;
cerr<<p.code<<endl;
cerr<<p.sqlstate<<endl;
cerr<<p.var_info<<endl;
}

return 0;
}

 

环境配置要求:

1、安装mysql数据库

2、安装mysql驱动,这里使用的是MyODBC-3.51.11-2-win

3、项目属性中c++的附加包含目录加上C:/WINDOWS/system32,因为myodbc3.dll,myodbc3.lib都在这里;

4、项目属性中链接器的附加库目录加上C:/WINDOWS/system32,原因同上