otl可通过odbc,数据库本身的连接库如oci,与数据库进行交互,跨平台,跨数据库,api使用方便且仅只是个头文件,我一直都使用这个。
otl的官网是:http://otl.sourceforge.net/ 里面例子文档什么的都相当全。
以otl连接oracle 11g为例,说明下在vs中的使用方式:
1. vs 编译环境设置
a. 在工程项目中引入otlv4.h头文件
b. 在vs中指定头文件目录:
c:\oracle\product\11.2.0\dbhome_1\oci\include
c.指定 附加库目录:
c:\oracle\product\11.2.0\dbhome_1\oci\lib\msvc
d. 输入附加库:
oci.lib
2. 以普通用户连接oracle数据库的例子:
#include #include #define otl_ora11g_r2 // compile otl 4.0/oci11.2#define otl_ora_utf8#include otlv4.h // include the otl 4 header file//#pragma comment(lib,oci.lib)using namespace std;otl_connect oracledb; int main(void){ //int otlsession_mode = oci_sysdba; try{ otl_connect::otl_initialize(); oracledb.rlogon(system/xcldb@xcldb); //....... }catch(otl_exception &p) { cerr<
b.因为oracle连接数据库较慢,有些会使用多线程,这时要注意线程安全问题.
通过otl_initialize()函数设不同的参数来解决.
// threaded_mode = 1 means the multi-threaded mode, 0 -- the single threaded mode
otl_connect::otl_initialize(0);
3. 用sysdba登录身份连接oracle数据库
当sys用户连接oracle时,如果用普通用户会报ora-28009 应当以sysdba或sysoper建立sys连接错误.
在session_begin中指定用户登录身份即可:
db.session_begin(m_struser.c_str(),m_strpassword.c_str(),0,oci_sysdba);
oci_default
oci_sysdba -- in this mode, the user is authenticated for sysdba access.
oci_sysoper -- in this mode, the user is authenticated for sysoper access.
