天天看點

OTL的使用

        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 <stdio.h>
#include <iostream>

#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/[email protected]");
       //.......
       
	}catch(otl_exception &p)
	{
		cerr<<p.msg<<endl;
		cerr<<p.stm_text<<endl;
		cerr<<p.var_info<<endl;
	}   
    oracledb.logoff();
	return 0;
}
           

    編譯注意事項:

        a. 如果使用的Oracle oci是64位的,vs就要編譯成64位的程式,如果編譯成32位,會提示找不到Oracle的動态庫。

         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.

 MAIL:  [email protected]

BLOG: http://blog.csdn.net/xcl168