使用OTL连接Oracle

来源:互联网 发布:linux连不上网 编辑:程序博客网 时间:2024/05/01 22:29

使用OTL连接Oracle9i,不用安装oracle客户端,直接连接Oracle,非常方便实用

步骤:

     1,下载一个instanceclient10_2,里面有一些dll文件,还有1个sdk的文件夹,把include和lib/msvc下的东西加入到VC工程目录里面

     2,就可以开始写1个Sample的程序了

   

  1. #include "otlv4.h"
  2. #define OTL_ORA9I
  3. otl_connect db;
  4. BOOL CheckConnect(CString ConnectStr, CString UserName, CString Password) 
  5. {
  6.     CString Msg;
  7.     try
  8.     {
  9.         db1.server_attach(ConnectStr);
  10.     }
  11.     catch(otl_exception& p)
  12.     {
  13.         Msg.Format("%d/n : %s",  p.code, p.msg);
  14.         MessageBox(Msg, NULL, MB_OK|MB_ICONSTOP);
  15.         return FALSE;
  16.     }
  17.     try
  18.     {
  19.         if(_stricmp(UserName.GetBuffer(),"sys") == 0)
  20.             db1.session_begin(UserName, Password, 0, OCI_SYSDBA);
  21.         else
  22.             db1.session_begin(UserName, Password, 0, OCI_DEFAULT);
  23.     }
  24.     catch(otl_exception)
  25.     {
  26.         db1.server_detach();
  27.         return FALSE;
  28.     }
  29.     return TRUE;
  30. }
  31. void retrieveDataFromDB()
  32. {
  33.     if (CheckConnect(connStr,userName,passwd)==TRUE)
  34.     {
  35.         CString Msg;
  36.         try
  37.         {
  38.             db1.set_max_long_size(20000);
  39.             otl_stream i(10, // buffer size. To read XML as CLOBs, it can be set to a size greater than 1
  40.                 "SELECT USERNAME FROM USERINFO",
  41.                 // SELECT statement
  42.                 db1 // connect object
  43.                 );
  44.             int listIndex = 0;
  45.             while(!i.eof()){ // while not end-of-data
  46.                 char *pUname = new char[21];                       
  47.                 i >> pUname;        
  48.                 listIndex++;
  49.                 delete []pUname;
  50.             }
  51.         }catch(otl_exception &p)
  52.         {
  53.             Msg.Format("%d/n : %s",  p.code, p.msg);
  54.             MessageBox(Msg, NULL, MB_OK|MB_ICONSTOP);
  55.         }
  56.     }else
  57.     {
  58.         MessageBox(_T("连接数据库失败!!"),_T("提示"),MB_ICONERROR);
  59.     }
  60. }

另外还有一些direct::exec()等执行,可以参照otl_example,另外,我发现9i出来的select count(*) .....,不能写到int里,好像数据结构不对

只能select to_char(count(*)) from ....,放到char[]里面了