C连接Oracle(转)

#include   "My_Ora_Oci.H"  
#include   <conio.h>  
   
  char   USER[31];  
  char   PASS_WORD[31];  
  char   SERVERNAME[31];  
   
  void   error_proc(dvoid   *errhp,   sword   status)  
  {  
      text   errbuf[512];  
      sb4   errcode;  
      switch   (status)  
      {  
          case   OCI_SUCCESS:  
            // printf("OCI_SUCCESS/n");  
            break;   
          case   OCI_SUCCESS_WITH_INFO:   
            printf("OCI   error:   OCI_SUCCESS_WITH_INFO/n");   
            break;   
          case   OCI_NEED_DATA:   
            printf("OCI   error:   OCI_NEED_DATA/n");  
            break;  
          case   OCI_NO_DATA:   
            printf("OCI   error:   OCI_NO_DATA/n");  
            break;   
         case   OCI_ERROR:   
           (void)OCIErrorGet((dvoid   *)errhp,(ub4)1,NULL,&errcode,  
                  errbuf,(ub4)sizeof(errbuf),OCI_HTYPE_ERROR);   
           printf("错误号:%d/n错误信息:%s/n",errcode,errbuf);  
           break;  
          case   OCI_INVALID_HANDLE:  
            printf("OCI   error:   OCI_INVALID_HANDLE/n");  
            break;  
          case   OCI_STILL_EXECUTING:  
            printf("OCI   error:   OCI_STILL_EXECUTING/n");  
            break;  
          default:  
             break;  
       }  
   
  }  
   
  void   main   ()  
  {   
        OCIEnv         *envhp   =   NULL; //环境句柄   
        OCIError *errhp   =   NULL; //错误句柄   
        OCIServer *srvhp   =   NULL; //服务器句柄   
        OCISvcCtx *svchp   =   NULL; //服务环境句柄   
        OCIStmt *stmthp   =   NULL; //语句句柄   
        OCIBind *bnd1p   =   (OCIBind   *)0; //结合句柄   
        OCIDefine *defhp[5]; //定义句柄   
        sword status;   
        int sum; //命中记录数   
        char*   SqlStat   =   "select   count(reader_barcode)   from   interll.readers   ";   
        int   li_ret;   
    
        li_ret   =   0;   
        strcpy(USER,"system");   
        strcpy(PASS_WORD,"manager");   
        strcpy(SERVERNAME,"melinets");  
   
  //     char   *SUMMARY_ELEMENT="   rec_ctrl_id,title,first_author,book_search_no,publisher";   
    
        //创建环境句柄   
        OCIEnvCreate(&envhp,OCI_DEFAULT,(dvoid   *)0,   
                 0,0,0,0,(dvoid   **)0);   
        //申请错误句柄   
        OCIHandleAlloc((dvoid   *)   envhp,(dvoid   **)&errhp,   
                OCI_HTYPE_ERROR,(size_t)0,(dvoid   **)0);   
         //申请服务器句柄   
        error_proc(errhp,OCIHandleAlloc((dvoid   *)envhp,(dvoid   **)&srvhp,   
                OCI_HTYPE_SERVER,(size_t)0,(dvoid   **)0));   
         //申请服务环境句柄   
        error_proc(errhp,OCIHandleAlloc((dvoid   *)envhp,(dvoid   **)&svchp,   
                OCI_HTYPE_SVCCTX,(size_t)0,(dvoid   **)0));   
        //设置服务环境的服务器属性   
        error_proc(errhp,OCIAttrSet((dvoid   *)svchp,OCI_HTYPE_SVCCTX,   
                (dvoid   *)srvhp,(ub4)0,OCI_ATTR_SERVER,(OCIError   *)errhp));   
        //连接数据库,建立会话   
        status   =   OCILogon(envhp,errhp,&svchp,USER,(ub4)strlen((char   *)USER),   
                (text   *)PASS_WORD,(ub4)strlen((char   *)PASS_WORD),(text   *)SERVERNAME,(ub4)strlen((char*) SERVERNAME));  
   
  if   (status==OCI_SUCCESS)   
        printf("成功连接melinets数据库,已为%s用户建立会话./n",USER);  
  else   {   
        printf("连接失败connect   fail!/n");   
        printf("-----ORA_search,ERROR   in   OCILogon-----/n");   
        error_proc(errhp,status);   
        return;  
  }  
  //申请语句句柄  
  error_proc(errhp,OCIHandleAlloc((dvoid   *)envhp,(dvoid   **)&stmthp,  
  OCI_HTYPE_STMT,(size_t)0,(dvoid   **)0));  
   
  //准备SQL语句  
  error_proc(errhp,OCIStmtPrepare(stmthp,errhp,SqlStat,  
      (ub4)strlen((char   *)   SqlStat),OCI_NTV_SYNTAX,OCI_DEFAULT));  
   
  //-------定义输出变量  
  error_proc(errhp,   OCIDefineByPos(stmthp,&defhp[0],errhp,1,∑,  
  sizeof(sum),SQLT_INT,(sb2   *)0,0,(ub2   *)0,OCI_DEFAULT)   );  
   
  // Ora_search(&li_ret);  
  // printf("li_ret   is   %d/n",li_ret);  
   
  //执行SQL语句  
  if   ((status=OCIStmtExecute(svchp,stmthp,errhp,(ub4)0,  
  (ub4)0,(CONST   OCISnapshot   *)NULL,   (OCISnapshot   *)NULL,  
  OCI_DEFAULT))  
  &&   status   !=   OCI_SUCCESS_WITH_INFO)  
  { //失败则退出  
  error_proc(errhp,status);  
  printf("-----ORA_search,ERROR   in   OCIStmtExecute-----/n");  
  return;  
  }  
  getch();  
   
  error_proc(errhp,OCIStmtFetch(stmthp,errhp,1,OCI_FETCH_NEXT,OCI_DEFAULT));  
   
  printf("The   sum   is   %d/n",sum);  
  //断开连接  
  error_proc(errhp,OCILogoff(svchp,errhp));  
      return   ;  
  } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值