CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

C++连Oracle数据库

楼主linton(漠然飞雪)2005-03-29 10:14:27 在 C/C++ / C++ 语言 提问

请问在C++中怎样连Oracle数据库,从数据表中读、写数据?急用!谢谢! 问题点数:60、回复次数:13Top

1 楼linton(漠然飞雪)回复于 2005-03-29 10:26:44 得分 0

高手快帮忙啊!Top

2 楼FlyWithJo(想飞)回复于 2005-03-29 11:05:21 得分 3

用ORACLE提供的OCI接口  
  或者pro*CTop

3 楼linton(漠然飞雪)回复于 2005-03-29 11:23:28 得分 0

我实现的思路是在Win32控制台基础上,从数据库中几个表中读出相关的数据参与计算,并将计算结果再写入数据库另一个表格。请问该怎么连?需要哪些头文件?最好能给出源码。谢谢了!Top

4 楼oyljerry(【勇敢的心】→ ㊣提拉米苏√㊣)回复于 2005-03-29 12:14:35 得分 3

利用Oracle提供的接口访问Top

5 楼zengwujun(月之海 为linux入门奋斗100天)回复于 2005-03-29 12:19:49 得分 5

pro*C  
  这里有一篇文章  
  http://community.csdn.net/Expert/topic/3540/3540486.xml?temp=.4882166  
  Top

6 楼zengwujun(月之海 为linux入门奋斗100天)回复于 2005-03-29 12:21:10 得分 0

而且oracle下面有示例demo/procTop

7 楼linton(漠然飞雪)回复于 2005-03-29 15:37:28 得分 0

还是不懂。为什么我将下面的引用放在"stdafx.h",结果会出错呢。  
   
  #ifndef   _AFX_NO_DB_SUPPORT  
  #include   <afxdb.h> //   MFC   ODBC   database   classes  
  #endif   //   _AFX_NO_DB_SUPPORT  
   
  #ifndef   _AFX_NO_DAO_SUPPORT  
  #include   <afxdao.h> //   MFC   DAO   database   classes  
  #endif   //   _AFX_NO_DAO_SUPPORT  
   
  提示错误是:  
  nafxcwd.lib(thrdcore.obj)   :   error   LNK2001:   unresolved   external   symbol   __endthreadex  
  nafxcwd.lib(thrdcore.obj)   :   error   LNK2001:   unresolved   external   symbol   __beginthreadex  
  Debug/LevelAdjust.exe   :   fatal   error   LNK1120:   2   unresolved   externals  
  这是什么原因啊?Top

8 楼chf()回复于 2005-03-29 16:10:37 得分 3

用ODBC   MFC   CRecordset类Top

9 楼zengwujun(月之海 为linux入门奋斗100天)回复于 2005-03-29 16:56:47 得分 3

你有vc,还用的着这么麻烦吗?  
  vc操作数据库的书和程序,找找,很多的Top

10 楼yuanming(清风)回复于 2005-03-29 17:07:30 得分 3

vc的话用ado  
  c的话pro*cTop

11 楼LoveSnowV(LoveSnowV)回复于 2005-03-29 18:11:02 得分 20

/******************************************************************************  
    *   Pro*   C/C++   SELECT   INSERT   演示程序  
    *  
    *   向joe_t2表插入测试数据,表生成准备脚本见当前目录ctScript.sql  
    *  
    *   黑龙江省哈尔滨市平房区  
    *   作者:高宏伟(DukeJoe)  
    *   2005-3-21   16:10  
    ******************************************************************************/  
  #include   <stdio.h>  
  #include   <sqlca.h>  
  #include   <stdarg.h>  
  #include   <time.h>  
  #include   <string.h>  
   
  #define           UNAME_LEN 20  
  #define           PWD_LEN 40  
  #define BUFFER_LENGTH 1024  
   
  void   sql_error(char   *msg)   ;  
  void   TRACE(char   *   pMsg)   ;  
  int   QueryCount()   ;  
  int   InsertData(int   iMaxF1,   int   iMaxF2)   ;  
   
  int   main()  
  {  
  /*   Declare   variables.No   declare   section   is   needed   if   MODE=ORACLE.*/  
  VARCHAR           username[UNAME_LEN];      
  varchar           password[PWD_LEN];          
  time_t   tmBegin,   tmEnd   ;  
  char   *   pc   ;  
  char   szBuffer[BUFFER_LENGTH]   ;  
  const   int   iMaxF1   =   10   ;  
  const   int   iMaxF2   =   5   ;  
   
  time(&tmBegin)   ;  
   
  /*   用户名口令   */  
          strncpy((char   *)   username.arr,   "billing",   UNAME_LEN);  
          username.len   =   strlen((char   *)   username.arr);  
          strncpy((char   *)   password.arr,   "billing",   PWD_LEN);  
          password.len   =   strlen((char   *)   password.arr);  
          /*   注册sql_error为错误处理函数   */  
          EXEC   SQL   WHENEVER   SQLERROR   DO   sql_error("ORACLE   error--\n");  
          /*   连接数据库   */  
          EXEC   SQL   CONNECT   :username   IDENTIFIED   BY   :password;  
          sprintf(szBuffer,   "\nConnected   to   ORACLE   as   user:   %s\n",   username.arr);  
           
          /**************************************************************************  
            *   为保证程序的连贯性,不必在SQL   Plus中经常truncate   table,  
            *   在程序中加入查询和DELETE的功能  
            *************************************************************************/  
          /*   查询当前的记录数   */  
  QueryCount()   ;  
  InsertData(iMaxF1,   iMaxF2)   ;  
   
          EXEC   SQL   COMMIT   WORK   RELEASE;  
   
  time(&tmEnd)   ;  
  pc   =   ctime(&tmBegin)   ;  
  printf("\n开始时间:%s\n",   pc)   ;  
  pc   =   ctime(&tmEnd)   ;  
  printf("结束时间:%s\n",   pc)   ;  
   
  return   0   ;  
  }  
   
  /******************************************************************************  
    *   系统跟踪函数(仿Microsoft   Visual   C++   TRACE   宏)  
    *   v1.0  
    *****************************************************************************/  
  void   TRACE(char   *   pMsg)  
  {  
  time_t   ttSimple   ;  
  char   szBuffer[64]   ;  
  struct   tm   *   ptmShow   ;  
   
  time(&ttSimple)   ;  
  ptmShow   =   localtime(&ttSimple)   ;  
   
  strftime(szBuffer,   64,   "%Y-%m-%d   %H:%M:%S",   ptmShow)   ;  
  printf("Logging(%s)-->%s\n",   szBuffer,   pMsg)   ;  
  }  
   
  int   QueryCount()  
  {  
  int   iCount   =   0   ;  
   
          EXEC   SQL   SELECT   count(*)   INTO   :iCount  
          FROM   joe_t2   ;  
           
          if   (   iCount   >   0   )  
          {  
          char   szBuffer[16]   ;  
          printf("在表joe_t2中发现%d条记录,如果不删除,可能因为主键约束的原因无法插入新值"  
          "。是否删除(y/n)?[Y]\t",   iCount)   ;  
          gets(szBuffer)   ;  
          switch(   *szBuffer   )  
          {  
          case   'n':  
          case   'N':  
          break   ;  
          default   :  
          EXEC   SQL   DELETE   FROM   joe_t2   ;  
          }  
          }  
           
          return   0   ;  
  }  
   
  int   InsertData(int   iMaxF1,   int   iMaxF2)  
  {  
  int   iF1,   iF2   ;  
  char   szBuffer[BUFFER_LENGTH]   ;  
   
  /*   插入数据   */  
  for   (   iF1   =   0   ;   iF1   <   iMaxF1   ;   iF1++   )  
  {  
  for   (   iF2   =   0   ;   iF2   <   iMaxF2   ;   iF2++   )  
  {  
  sprintf(szBuffer,   "第   %3d   条记录",   iF1*iMaxF2+iF2)   ;  
  TRACE(szBuffer)   ;  
  EXEC   SQL   INSERT   INTO   joe_t2(f1,   f2,   f3,   f4)  
  VALUES   (   :iF1,   :iF2,   :szBuffer,   sysdate)   ;  
  }  
  }  
   
  return   0   ;  
  }  
   
  /******************************************************************************  
    *   错误处理函数  
    *****************************************************************************/  
  void   sql_error(char   *msg)  
  {  
          char   err_msg[128];  
          int   buf_len,   msg_len;  
   
          EXEC   SQL   WHENEVER   SQLERROR   CONTINUE;  
          printf("\n%s\n",   msg);  
          buf_len   =   sizeof   (err_msg);  
          sqlglm(err_msg,   &buf_len,   &msg_len);  
          printf("%.*s\n",   msg_len,   err_msg);  
          EXEC   SQL   ROLLBACK   RELEASE;  
          exit(1);  
  }  
  /*   这是一个前几天写的Pro*C/C++程序,看懂它你就会用C++连接ORACLE了   */Top

12 楼LoveSnowV(LoveSnowV)回复于 2005-03-29 18:13:02 得分 20

//   BeingConnectionStringCpp  
  #import   "C:\Program   Files\Common   Files\System\ADO\msado15.dll"   \  
          no_namespace   rename("EOF",   "EndOfFile")  
   
  #include   <ole2.h>  
  #include   <stdio.h>  
  #include   <conio.h>  
   
  //   Function   declarations  
  inline   void   TESTHR(HRESULT   x)   {if   FAILED(x)   _com_issue_error(x);};  
  void   ConnectionStringX();  
  _bstr_t   GetState(int   intState);    
  void   PrintProviderError(_ConnectionPtr   pConnection);  
  void   PrintComError(_com_error   &e);  
   
  ///////////////////////////////////////////////////////////  
  //                                                                                                               //  
  //             Main   Function                                                                         //  
  //                                                                                                               //  
  ///////////////////////////////////////////////////////////  
   
  void   main()  
  {  
          if(FAILED(::CoInitialize(NULL)))  
                  return;  
   
          ConnectionStringX();  
   
          //Wait   here   for   user   to   see   the   output..  
          printf("\nPress   any   key   to   continue...");  
          getch();  
   
          ::CoUninitialize();  
  }  
   
  ///////////////////////////////////////////////////////////  
  //                                                                                                               //  
  //             ConnectionStringX   Function                                               //  
  //                                                                                                               //  
  ///////////////////////////////////////////////////////////  
   
  void   ConnectionStringX()  
  {  
          //   Define   Connection   object   pointers.  
          //   Initialize   pointers   on   define.  
          //   These   are   in   the   ADODB::     namespace  
            _ConnectionPtr   pConnection1   =   NULL;  
            _ConnectionPtr   pConnection2   =   NULL;  
            _ConnectionPtr   pConnection3   =   NULL;  
            _ConnectionPtr   pConnection4   =   NULL;  
   
          //Define   Other   Variables  
          HRESULT     hr   =   S_OK;  
   
          try  
          {  
                  //   Open   a   connection   using   OLE   DB   syntax.  
                  TESTHR(pConnection1.CreateInstance(__uuidof(Connection)));  
                  pConnection1->ConnectionString   =    
                          "Provider='sqloledb';Data   Source='MySqlServer';"  
                          "Initial   Catalog='Pubs';Integrated   Security='SSPI';";  
                  pConnection1->ConnectionTimeout   =   30;  
                  pConnection1->Open("","","",adConnectUnspecified);  
                  printf("cnn1   state:   %s\n",    
                          (LPCTSTR)GetState(pConnection1->State));  
                   
                  //   Open   a   connection   using   a   DSN   and   ODBC   tags.  
                  //   It   is   assumed   that   you   have   create   DSN   'Pubs'   with   a   user   name   as    
                  //   'MyUserId'   and   password   as   'MyPassword'.  
                  TESTHR(pConnection2.CreateInstance(__uuidof(Connection)));  
                  pConnection2->ConnectionString   =   "DSN=Pubs;UID=MyUserId;PWD=MyPassword;";  
                  pConnection2->Open("","","",adConnectUnspecified);  
                  printf("cnn2   state:   %s\n",    
                          (LPCTSTR)GetState(pConnection2->State));  
   
                  //   Open   a   connection   using   a   DSN   and   OLE   DB   tags.  
                  TESTHR(pConnection3.CreateInstance(__uuidof(Connection)));  
                  pConnection3->ConnectionString   =   "Data   Source=Pubs;";  
                  pConnection3->Open("","","",adConnectUnspecified);  
                  printf("cnn3   state:   %s\n",    
                          (LPCTSTR)GetState(pConnection3->State));  
   
                  //   Open   a   connection   using   a   DSN   and   individual  
                  //   arguments   instead   of   a   connection   string.  
                  //   It   is   assumed   that   you   have   create   DSN   'Pubs'   with   a   user   name   as    
                  //   'MyUserId'   and   password   as   'MyPassword'.  
                  TESTHR(pConnection4.CreateInstance(__uuidof(Connection)));  
                  pConnection4->Open("Pubs","MyUserId","MyPassword",adConnectUnspecified);  
                  printf("cnn4   state:   %s\n",    
                          (LPCTSTR)GetState(pConnection4->State));  
          }  
          catch(_com_error   &e)  
          {  
                  //   Notify   user   of   any   errors.  
                  //   Pass   a   connection   pointer   accessed   from   the   Connection.  
                  PrintProviderError(pConnection1);  
                  if(pConnection2)  
                          PrintProviderError(pConnection2);  
                  if(pConnection3)  
                          PrintProviderError(pConnection3);  
                  if(pConnection4)  
                          PrintProviderError(pConnection4);  
                  PrintComError(e);  
          }  
   
          //Cleanup   objects   before   exit.  
          if   (pConnection1)  
                  if   (pConnection1->State   ==   adStateOpen)  
                          pConnection1->Close();  
          if   (pConnection2)  
                  if   (pConnection2->State   ==   adStateOpen)  
                          pConnection2->Close();  
          if   (pConnection3)  
                  if   (pConnection3->State   ==   adStateOpen)  
                          pConnection3->Close();  
          if   (pConnection4)  
                  if   (pConnection4->State   ==   adStateOpen)  
                          pConnection4->Close();  
  }  
   
  ///////////////////////////////////////////////////////////  
  //                                                                                                               //  
  //             GetState   Function                                                                 //  
  //                                                                                                               //  
  ///////////////////////////////////////////////////////////  
   
  _bstr_t   GetState(int   intState)    
  {  
          _bstr_t   strState;    
          switch(intState)    
          {  
                  case   adStateClosed:  
                          strState   =   "adStateClosed";  
                          break;  
                  case   adStateOpen:  
                          strState   =   "adStateOpen";  
                          break;  
                  default:  
                  ;  
          }  
          return   strState;  
  }  
   
  ///////////////////////////////////////////////////////////  
  //                                                                                                               //  
  //             PrintProviderError   Function                                             //  
  //                                                                                                               //  
  ///////////////////////////////////////////////////////////  
   
  void   PrintProviderError(_ConnectionPtr   pConnection)  
  {  
          //   Print   Provider   Errors   from   Connection   object.  
          //   pErr   is   a   record   object   in   the   Connection's   Error   collection.  
          ErrorPtr     pErr   =   NULL;  
   
          if(   (pConnection->Errors->Count)   >   0)  
          {  
                  long   nCount   =   pConnection->Errors->Count;  
   
                  //   Collection   ranges   from   0   to   nCount   -1.  
                  for(long   i   =   0;   i   <   nCount;   i++)  
                  {  
                          pErr   =   pConnection->Errors->GetItem(i);  
                          printf("Error   number:   %x\t%s\n",   pErr->Number,    
                                  (LPCSTR)pErr->Description);  
                  }  
          }  
  }  
   
  ///////////////////////////////////////////////////////////  
  //                                                                                                               //  
  //             PrintComError   Function                                                       //  
  //                                                                                                               //  
  ///////////////////////////////////////////////////////////  
   
  void   PrintComError(_com_error   &e)  
  {  
          _bstr_t   bstrSource(e.Source());  
          _bstr_t   bstrDescription(e.Description());  
   
          //   Print   Com   errors.      
          printf("Error\n");  
          printf("\tCode   =   %08lx\n",   e.Error());  
          printf("\tCode   meaning   =   %s\n",   e.ErrorMessage());  
          printf("\tSource   =   %s\n",   (LPCSTR)   bstrSource);  
          printf("\tDescription   =   %s\n",   (LPCSTR)   bstrDescription);  
  }  
  //   EndConnectionStringCpp  
  /*   这是一个ADO的例子,看懂它,你一样可以连接ORACLE,不是能不能连,就看你想不想看懂这两个程序了,希望对你有用,祝你好运   */Top

13 楼linton(漠然飞雪)回复于 2005-04-13 21:55:01 得分 0

谢谢!Top

相关问题

  • C/C++连接oracle数据库问题
  • c#如何连接ORACLE数据库
  • C#打开Oracle数据库,怎么写?
  • 请问用C#连结ORACLE数据库
  • asp.net(c#)备份Oracle数据库?
  • c#的oracle数据库操作?update、insert?
  • C#与ORACLE数据库的连接
  • 用C/c++语言怎样在Linux下连接oracle数据库!
  • 用PRO*C 做Oracle 数据库应用开发本地必须装 oracle 数据库服务器吗?
  • 怎样在C环境下链接Oracle数据库?

关键词

  • c++
  • 数据库
  • oracle
  • 数据
  • mfc
  • sql
  • connectionstringx
  • imaxf
  • arr
  • uname

得分解答快速导航

  • 帖主:linton
  • FlyWithJo
  • oyljerry
  • zengwujun
  • chf
  • zengwujun
  • yuanming
  • LoveSnowV
  • LoveSnowV

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo