oracle版本为oracle9i客户端精简版,编译C++工具为VC++6.0
代码如下:#include <iostream>
using namespace std;
#include <stdio.h>#define OTL_ORA9I // Compile OTL 4.0/OCI9i
#define OTL_UNICODE // Enable Unicode OTL for OCI9i
#include <otlv4.h> // include the OTL 4.0 header fileotl_connect db; // connect objectvoid insert()
// insert rows into table

otl_stream o(50, // buffer size
              "insert into test_tab values(:f1<float>,:f2<char[31]>;)", 
                 // SQL statement
              db // connect object
             );
char tmp[32];
unsigned short tmp2[32]; // Null terminated Unicode character array.for(int i=1;i<=100;++i){
#if defined(_MSC_VER)
#if (_MSC_VER >= 1400) // VC++ 8.0 or higher
  sprintf_s(tmp,sizeof(tmp),"Name%d",i);
#else
  sprintf(tmp,"Name%d",i);
#endif
#else
  sprintf(tmp,"Name%d",i);
#endif
  unsigned short* c2=tmp2;
  char* c1=tmp;
// Unicode's first 128 characters are ASCII (0..127), so
// all is needed for converting ASCII into Unicode is as follows:
  while(*c1){
    *c2=OTL_SCAST(unsigned char,*c1);
   ++c1; ++c2;   
  }  
  *c2=0; // target Unicode string is null terminated,
         // only the null terminator is a two-byte character, 
         // not one-byte
  o<<static_cast<float>(i);
  o<<reinterpret_cast<unsigned char*>(tmp2); 
   // overloaded operator<<(const unsigned char*) in the case of Unicode
   // OTL accepts a pointer to a Unicode character array.
   // operator<<(const unsigned short*) wasn't overloaded
   // in order to avoid ambiguity in C++ type casting.
}}void select()

otl_stream i(50, // buffer size
              "select * from test_tab where f1>=:f<int> and f1<=:f*2",
                 // SELECT statement
              db // connect object
             ); 
   // create select streamfloat f1;
unsigned short f2[32];i<<8; // assigning :f = 8
   // SELECT automatically executes when all input variables are
   // assigned. First portion of output rows is fetched to the bufferwhile(!i.eof()){ // while not end-of-data
  i>>f1;
  i>>reinterpret_cast<unsigned char*>(f2);
    // overloaded operator>>(unsigned char*) in the case of Unicode
    // OTL accepts a pointer to a Unicode chracter array.
    // operator>>(unsigned short*) wasn't overloaded 
    // in order to avoid ambiguity in C++ type casting.
  cout<<"f1="<<f1<<", f2=";
// Unicode's first 128 characters are ASCII, so in order
// to convert Unicode back to ASCII all is needed is
// as follows:
   for(int j=0;f2[j]!=0;++j){
     cout<<static_cast<char>(f2[j]);
   }
   cout<<endl;
}i<<4; // assigning :f = 4
   // SELECT automatically executes when all input variables are
   // assigned. First portion of output rows is fetched to the bufferwhile(!i.eof()){ // while not end-of-data
   i>>f1>>reinterpret_cast<unsigned char*>(f2);
  cout<<"f1="<<f1<<", f2=";
   for(int j=0;f2[j]!=0;++j){
     cout<<static_cast<char>(f2[j]);
   }
   cout<<endl;
}}int main()
{
otl_connect::otl_initialize(); // initialize OCI environment
try{  db.rlogon("scott/tiger"); // connect to Oracle  otl_cursor::direct_exec
   (
    db,
    "drop table test_tab",
    otl_exception::disabled // disable OTL exceptions
   ); // drop table  otl_cursor::direct_exec
   (
    db,
    "create table test_tab(f1 number, f2 varchar2(30))"
    );  // create table  insert(); // insert records into table
  select(); // select records from table}catch(otl_exception& p){ // intercept OTL exceptions
  cerr<<p.msg<<endl; // print out error message
  cerr<<p.stm_text<<endl; // print out SQL that caused the error
  cerr<<p.var_info<<endl; // print out the variable that caused the error
}db.logoff(); // disconnect from Oraclereturn 0;}
编译后缺少头文件oci.h,我也知道我装的是精简版的oracle,问题是我装的是 win7系统,装了2次oracle9i的完整版客户端装不上,在实在没办法的情况下我只能把同事装过9i完整版下的oci目录(包括oci.h的include 和oci.lib的lib)拷贝到我的oracle目录里,并且vc编译器里设置好路径,在把otlv4.h放入vc的inlucde目录里,最后在编译不报缺少oci.h的错误了,但出现以下错误:
mytest1.obj : error LNK2001: unresolved external symbol _OCIHandleFree
mytest1.obj : error LNK2001: unresolved external symbol _OCIServerDetach
mytest1.obj : error LNK2001: unresolved external symbol _OCISessionEnd
mytest1.obj : error LNK2001: unresolved external symbol _OCIBindByName
mytest1.obj : error LNK2001: unresolved external symbol _OCIAttrGet
mytest1.obj : error LNK2001: unresolved external symbol _OCIStmtFetch
mytest1.obj : error LNK2001: unresolved external symbol _OCIInitialize
mytest1.obj : error LNK2001: unresolved external symbol _OCIAttrSet
mytest1.obj : error LNK2001: unresolved external symbol _OCIServerAttach
mytest1.obj : error LNK2001: unresolved external symbol _OCIHandleAlloc
mytest1.obj : error LNK2001: unresolved external symbol _OCIEnvInit
mytest1.obj : error LNK2001: unresolved external symbol _OCISessionBegin
mytest1.obj : error LNK2001: unresolved external symbol _OCIStmtExecute
mytest1.obj : error LNK2001: unresolved external symbol _OCIStmtPrepare
mytest1.obj : error LNK2001: unresolved external symbol _OCIDefineByPos
mytest1.obj : error LNK2001: unresolved external symbol _OCIParamGet
mytest1.obj : error LNK2001: unresolved external symbol _OCIErrorGet
mytest1.obj : error LNK2001: unresolved external symbol _OCIDescriptorFree
mytest1.obj : error LNK2001: unresolved external symbol _OCIDescriptorAlloc
mytest1.obj : error LNK2001: unresolved external symbol _OCITransCommit
mytest1.obj : error LNK2001: unresolved external symbol _OCILobWrite请大哥们帮帮小弟,怎么解决?谢谢了

解决方案 »

  1.   

    你是用OCI来访问Oracle数据库呀?OCI太麻烦了,建议用ADO吧,很简单,不过程序运行时得安装Oracle客户端,我就是这么弄的,比较好使。
      

  2.   

    http://www.diybl.com/course/7_databases/oracle/oraclejs/2008921/144117.html
    给   或多或少有帮助
      

  3.   

    谢谢楼上的2位,其实方法都知道了,就是自己用的win7系统装不了oracle完整版的,哎。
      

  4.   

    还是没有解决问题吗?
    你实际上的配置的问题:
    1、project->setting->Link object/library modules中,在最后处加上oci.lib
    2、tools->option->Directories 
    show directories for 
        选择"Include files"时,请在其下方Directories中添加otl头文件及oracle之oci库路径所在目录所在的目录,类似如下:
                      D:\Programs\Oracle10gEn\OCI\include
                      E:\Work\otl
        选择"Library files"时,添加OCI.lib的路径,类似如下:
                      D:\Programs\Oracle10gEn\OCI\lib\MSVC
                      D:\Programs\Oracle10gEn\OCI\lib\MSVC\vc7
                      D:\Programs\Oracle10gEn\OCI\lib\MSVC\vc71你的问题是没有加oci.lib的缘故。
      

  5.   

    这是vc6.0+otlv4.h+oracle10.2的一个示例:
    #include <iostream>
    using namespace std;#include <stdio.h>#define OTL_ORA10G
    #include <otlv4.h> // include the OTL 4.0 header fileotl_connect db; // connect objectvoid insert()
    // insert rows into table

    otl_stream o(50,
    "insert into test_tab values(:f1<float>,:f2<char[31]>)", 
    db 
    );
    char tmp[32];

    for(int i=1;i<=100;++i){
    sprintf(tmp,"Name%d",i);
    o<<(float)i<<tmp;
    }
    }void select()

    otl_stream i(50,
    "select * from test_tab where f1>=:f<int> and f1<=:f*2",
    db 
    ); 
    // create select stream

    float f1;
    char f2[31];

    i<<4; 

    while(!i.eof()){ // while not end-of-data
    i>>f1>>f2;
    cout<<"f1="<<f1<<", f2="<<f2<<endl;
    }

    }int main()
    {
    otl_connect::otl_initialize(); // initialize OCI environment
    try
    {
    db.rlogon("system/scott"); // connect to Oracle

    otl_cursor::direct_exec
    (
    db,
    "drop table test_tab",
    otl_exception::disabled // disable OTL exceptions
    ); // drop table

    otl_cursor::direct_exec
    (
    db,
    "create table test_tab(f1 number, f2 varchar2(30))"
    );  // create table

    insert(); // insert records into table

    db.logoff(); // disconnect from Oracle

    db.server_attach(); // attach to the local Oracle server
    // In order to connect to a remote server,
    // a TNS alias needs to be specified

    for(int i=1;i<=100;++i)
    {
    cout<<"Session begin ==> "<<i<<endl;
    db.session_begin("scott","tiger");

    select(); // select records from table

    cout<<"Session end ==> "<<i<<endl;
    db.session_end(); // end session
    }

    db.server_detach(); // detach from the Oracle server
    }
    catch(otl_exception& p){ // intercept OTL exceptions
    cerr<<p.msg<<endl; // print out error message
    cerr<<p.stm_text<<endl; // print out SQL that caused the error
    cerr<<p.var_info<<endl; // print out the variable that caused the error
    }

    db.logoff(); // make sure that the program gets disconnected from Oracle

    return 0;

    }