数据库操作方式:可以采用ADO方式,也可以采用oracle本身提供的Proc*C/C++或者是OCCI方式操作数据库。连接方式:可以是客户端连接、也可以是服务器端连接。数据库配置:无论是何种连接都需要进行数据库连接的配置,一般在ORACLE_HOME下面的network/admin/tnsnames.ora文件中进行配置,如果没有此目录或者是此文件,需要自己手工添加。内容格式大致如下:点击(此处)折叠或打开1.BM2D0=2.(DESCRIPTION=3.(ADDRESS_LIST=4.(ADDRESS=(PROTOCOL=TCP)(HOST=XXX.XXX.XXX.XXX)(PORT=1521))5.)6.(CONNECT_DATA=7.(SERVICE_NAME=BM2D0)8.)9.)其中橄榄色可任意起名,一般在数据库连接是作为服务和用户名、密码一起确定数据库连接的参数。第一个鲜粉色是远程oracle数据库所在服务器的IP地址,端口号一般为1521。第二个鲜粉色是远程oracle所在主机的全局数据库名字,不能随意更改。后两个搭配起来能够确定唯一连接对象。客户端连接:方式一:ADOmain.cpp点击(此处)折叠或打开1.#includeDBOperation.h2.#includeiostream3.usingnamespacestd;4.5.voidmain()6.{7.CDBOperationdbOper;8.boolbConn=dbOper.ConnToDB(Provider=OraOLEDB.Oracle.1;PersistSecurityInfo=True;DataSource=xxx1,xxx2,xxx3);9.if(false==bConn)10.{11.printf(连接数据库出现错误\n);12.system(PAUSE);13.return;14.}15.16._RecordsetPtrpRst;17.18.//执行查询语句19.//char*sql=select*fromTSTUDENT;20.charsql[255]={0};21.strcpy(sql,select*fromTSTUDENT);22.pRst=dbOper.ExecuteWithResSQL(sql);23.if(NULL==pRst)24.{25.printf(查询数据出现错误!\n);26.system(PAUSE);27.return;28.}29.if(pRst-adoEOF)30.{31.pRst-Close();32.printf(Thereisnorecordsinthistable\n);33.return;34.}35._variant_tvSno,vName,v***,vAge,vDno,vDname,vCname;36.while(!pRst-adoEOF)37.{38.//pRst-MoveFirst();//记录集指针移动到查询结果集的前面39.vSno=pRst-GetCollect(_variant_t((long)0));40.vName=pRst-GetCollect(_variant_t(name));41.v***=pRst-GetCollect(_variant_t(***));42.vAge=pRst-GetCollect(_variant_t(age));43.//vDno=pRst-GetCollect(dno);44.//vDname=pRst-GetCollect(dname);45.//vCname=pRst-GetCollect(cname);46.47.printf(%s\t%s\t%s\t%d\n,(LPSTR)(LPCSTR)(_bstr_t)vSno,(LPSTR)(LPCSTR)_bstr_t(vName),(LPSTR)(LPCSTR)_bstr_t(v***),vAge.intVal);48.pRst-MoveNext();49.}50.51.//执行插入语句52.//sprintf(sql,insertintoTSTUDENT(sno,name,***,age)values('%s','%s','%s',%d),20080016,全局,女,25);53.strcpy(sql,insertintoTSTUDENT(sno,name,***,age)values('20080001','全局','女',25));54.pRst=dbOper.ExecuteWithResSQL(sql);55.if(NULL!=pRst)56.{57.printf(插入数据成功\n);58.}59.//执行删除语句60.61.sprintf(sql,deletefromTSTUDENTwheresno='%s',20080017);62.pRst=dbOper.ExecuteWithResSQL(sql);63.if(NULL!=pRst)64.{65.printf(删除数据成功\n);66.}67.system(PAUSE);68.//pRst-Close();69.}其中XXX1:是tnsnames.ora中配置的服务名,XXX2是用户名,XXX3是密码。DBOperation.h:点击(此处)折叠或打开1.#pragmaonce2.#importc:\programfiles\commonfiles\system\ado\msado15.dllno_namespacerename(EOF,adoEOF)3.classCDBOperation4.{5.public:6.//初始化数据库操作需要的对象7.CDBOperation(void);8.~CDBOperation(void);9.//连接至数据库10.boolConnToDB(char*ConnectionString,char*UserID,char*Password);11.12.//数据库操作函数13.//查询操作删除以及添加14._RecordsetPtrExecuteWithResSQL(constchar*);15.//boolExecuteNoResSQL(constchar*);//deleteandadd16.17.private:18.voidPrintErrorInfo(_com_error&);19.20.private:21.//初始化数据库连接、命令、记录集22._ConnectionPtrCreateConnPtr();23._CommandPtrCreateCommPtr();24._RecordsetPtrCreateRecsetPtr();25.26.private:27.//数据库连接需要的连接、命令操作对象28._ConnectionPtrm_pConnection;29._CommandPtrm_pCommand;30.};DBOperation.cpp点击(此处)折叠或打开1.#includeDBOperation.h2.3.CDBOperation::CDBOperation(void)4.{5.CoInitialize(NULL);6.m_pConnection=CreateConnPtr();7.m_pCommand=CreateCommPtr();8.}9.10.CDBOperation::~CDBOperation(void)11.{12.//m_pCommand-Close();13.m_pConnection-Close();14.}15.16.boolCDBOperation::ConnToDB(char*ConnectionString,char*UserID,char*Password)17.{18.if(NULL==m_pConnection)19.{20.printf(Failedtocreateconnection\n);21.returnfalse;22.}23.24.try25.{26.HRESULThr=m_pConnection-Open(ConnectionString,UserID,Password,NULL);27.if(TRUE==FAILED(hr))28.{29.returnfalse;30.}31.m_pCommand-ActiveConnection=m_pConnection;32.returntrue;33.}34.catch(_com_error&e)35.{36.PrintErrorInfo(e);37.returnfalse;38.}39.}40.41._RecordsetPtrCDBOperation::ExecuteWithResSQL(constchar*sql)42.{43.//已经在连接至数据库的时候进行判断了44.//if(NULL==m_pCommand||0==m_pConnection-State)45.//{46.//printf(FailedtocreatecommandORthestateofconnectioniszero\n);47.//returnNULL;48.//}49.50.//char*query=newchar;51.//strcpy(query,sql);52.try53.{54.m_pCommand-CommandText=_bstr_t(sql);55._RecordsetPtrpRst=m_pCommand-Execute(NULL,NULL,adCmdText);56.returnpRst;57.//_variant_tra;58.//_RecordsetPtrpRst=m_pConnection-Execute((_bstr_t)query,&ra,adCmdText);59.}60.catch(_com_error&e)61.{62.PrintErrorInfo(e);63.returnNULL;64.}65.}66.67.//boolCDBOperation::ExecuteNoResSQL(constchar*sql)68.//{69.////if(NULL==m_pCommand||0==m_pConnection-State)70.////{71.////printf();72.////}73.//try74.//{75.//char*query=NULL;76.//strcpy(query,sql);77.//m_pCommand-CommandText=(_bstr_t)query;78.//79.//}80.//}81.82.voidCDBOperation::PrintErrorInfo(_com_error&e)83.{84.printf(Errorinfomationareasfollows\n);85.printf(ErrorNo:%d\nErrorMessage:%s\nErrorSource:%s\nErrorDescription:%s\n,e.Error(),e.ErrorMessage(),(LPCTSTR)e.Source(),(LPCTSTR)e.Description());86.}87.88._ConnectionPtrCDBOperation::CreateConnPtr()89.{90.HRESULThr;91._ConnectionPtrconnPtr;92.hr=connPtr.CreateInstance(__uuidof(Connection));93.if(FAILED(hr)==TRUE)94.{95.returnNULL;96.}97.returnconnPtr;98.}99.100._CommandPtrCDBOperation::CreateCommPtr()101.{102.HRESULThr;103._CommandPtrcommPtr