數據庫操作方式:可以采用ADO方式,也可以采用oracle本身提供的Proc*C/C++或者是OCCI方式操作數據庫。
連接方式:可以是客戶端連接、也可以是服務器端連接。
數據庫配置:無論是何種連接都需要進行數據庫連接的配置,一般在ORACLE_HOME下面的network/admin/tnsnames.ora文件中進行配置,如果沒有此目錄或者是此文件,需要自己手工添加。內容格式大致如下:
點擊(此處)折疊或打開
- BM2D0 =
- (DESCRIPTION =
- (ADDRESS_LIST =
- (ADDRESS = (PROTOCOL = TCP)(HOST = XXX.XXX.XXX.XXX)(PORT = 1521))
- )
- (CONNECT_DATA =
- (SERVICE_NAME = BM2D0)
- )
- )
其中
橄欖色可任意起名,一般在數據庫連接是作為服務和用戶名、密碼一起確定數據庫連接的參數。
第一個鮮粉色是遠程oracle數據庫所在服務器的IP地址,端口號一般為1521。
第二個鮮粉色是遠程oracle所在主機的全局數據庫名字,不能隨意更改。
后兩個搭配起來能夠確定唯一連接對象。
客戶端連接:
客戶端連接:
方式一:ADO
main.cpp
點擊(此處)折疊或打開
- #include "DBOperation.h"
- #include <iostream>
- using namespace std;
- void main()
- {
- CDBOperation dbOper;
- bool bConn = dbOper.ConnToDB("Provider=OraOLEDB.Oracle.1;Persist Security Info=True;Data Source=xxx1", "xxx2", "xxx3");
- if (false == bConn)
- {
- printf("連接數據庫出現錯誤\n");
- system("PAUSE");
- return;
- }
- _RecordsetPtr pRst;
- //執行查詢語句
- //char *sql = "select * from TSTUDENT";
- char sql[255] = {0};
- strcpy(sql, "select * from TSTUDENT");
- pRst = dbOper.ExecuteWithResSQL(sql);
- if (NULL == pRst)
- {
- printf("查詢數據出現錯誤!\n");
- system("PAUSE");
- return;
- }
- if (pRst->adoEOF)
- {
- pRst->Close();
- printf("There is no records in this table\n");
- return;
- }
- _variant_t vSno, vName, v***, vAge, vDno, vDname, vCname;
- while (!pRst->adoEOF)
- {
- //pRst->MoveFirst(); //記錄集指針移動到查詢結果集的前面
- vSno = pRst->GetCollect(_variant_t((long)0));
- vName = pRst->GetCollect(_variant_t("name"));
- v*** = pRst->GetCollect(_variant_t("***"));
- vAge = pRst->GetCollect(_variant_t("age"));
- //vDno = pRst->GetCollect("dno");
- //vDname = pRst->GetCollect("dname");
- //vCname = pRst->GetCollect("cname");
- 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);
- pRst->MoveNext();
- }
- //執行插入語句
- //sprintf(sql, "insert into TSTUDENT(sno, name, ***, age) values('%s', '%s', '%s', %d)", "20080016", "全局", "女", 25);
- strcpy(sql, "insert into TSTUDENT(sno, name, ***, age) values('20080001', '全局', '女', 25)");
- pRst = dbOper.ExecuteWithResSQL(sql);
- if (NULL != pRst)
- {
- printf("插入數據成功\n");
- }
- //執行刪除語句
- sprintf(sql, "delete from TSTUDENT where sno = '%s'", "20080017");
- pRst = dbOper.ExecuteWithResSQL(sql);
- if (NULL != pRst)
- {
- printf("刪除數據成功\n");
- }
- system("PAUSE");
- //pRst->Close();
- }
其中XXX1:是tnsnames.ora中配置的服務名,XXX2是用戶名,XXX3是密碼。
DBOperation.h:
點擊(此處)折疊或打開
- #pragma once
- #import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "adoEOF")
- class CDBOperation
- {
- public:
- //初始化數據庫操作需要的對象
- CDBOperation(void);
- ~CDBOperation(void);
- //連接至數據庫
- bool ConnToDB(char *ConnectionString, char *UserID, char *Password);
- //數據庫操作函數
- //查詢操作 刪除以及添加
- _RecordsetPtr ExecuteWithResSQL(const char *);
- //bool ExecuteNoResSQL(const char *);//delete and add
- private:
- void PrintErrorInfo(_com_error &);
- private:
- //初始化數據庫連接、命令、記錄集
- _ConnectionPtr CreateConnPtr();
- _CommandPtr CreateCommPtr();
- _RecordsetPtr CreateRecsetPtr();
- private:
- //數據庫連接需要的連接、命令操作對象
- _ConnectionPtr m_pConnection;
- _CommandPtr m_pCommand;
- };
DBOperation.cpp
點擊(此處)折疊或打開
- #include "DBOperation.h"
- CDBOperation::CDBOperation(void)
- {
- CoInitialize(NULL);
- m_pConnection = CreateConnPtr();
- m_pCommand = CreateCommPtr();
- }
- CDBOperation::~CDBOperation(void)
- {
- //m_pCommand->Close();
- m_pConnection->Close();
- }
- bool CDBOperation::ConnToDB(char *ConnectionString, char *UserID, char *Password)
- {
- if (NULL == m_pConnection)
- {
- printf("Failed to create connection\n");
- return false;
- }
- try
- {
- HRESULT hr = m_pConnection->Open(ConnectionString, UserID, Password, NULL);
- if (TRUE == FAILED(hr))
- {
- return false;
- }
- m_pCommand->ActiveConnection = m_pConnection;
- return true;
- }
- catch(_com_error &e)
- {
- PrintErrorInfo(e);
- return false;
- }
- }
- _RecordsetPtr CDBOperation::ExecuteWithResSQL(const char *sql)
- {
- //已經在連接至數據庫的時候進行判斷了
- //if (NULL == m_pCommand || 0 == m_pConnection->State)
- //{
- // printf("Failed to create command OR the state of connection is zero\n");
- // return NULL;
- //}
- //char *query = new char;
- //strcpy(query, sql);
- try
- {
- m_pCommand->CommandText = _bstr_t(sql);
- _RecordsetPtr pRst = m_pCommand->Execute(NULL, NULL, adCmdText);
- return pRst;
- //_variant_t ra;
- //_RecordsetPtr pRst = m_pConnection->Execute((_bstr_t)query, &ra, adCmdText);
- }
- catch(_com_error &e)
- {
- PrintErrorInfo(e);
- return NULL;
- }
- }
- //bool CDBOperation::ExecuteNoResSQL(const char *sql)
- //{
- // //if (NULL == m_pCommand || 0 == m_pConnection->State)
- // //{
- // // printf();
- // //}
- // try
- // {
- // char *query = NULL;
- // strcpy(query, sql);
- // m_pCommand->CommandText = (_bstr_t)query;
- //
- // }
- //}
- void CDBOperation::PrintErrorInfo(_com_error &e)
- {
- printf("Error infomation are as follows\n");
- printf("ErrorNo: %d\nError Message:%s\nError Source:%s\nError Description:%s\n", e.Error(), e.ErrorMessage(), (LPCTSTR)e.Source(), (LPCTSTR)e.Description());
- }
- _ConnectionPtr CDBOperation::CreateConnPtr()
- {
- HRESULT hr;
- _ConnectionPtr connPtr;
- hr = connPtr.CreateInstance(__uuidof(Connection));
- if (FAILED(hr) == TRUE)
- {
- return NULL;
- }
- return connPtr;
- }
- _CommandPtr CDBOperation::CreateCommPtr()
- {
- HRESULT hr;
- _CommandPtr commPtr;
- hr = commPtr.CreateInstance(__uuidof(Command));
- if (FAILED(hr) == TRUE)
- {
- return NULL;
- }
- return commPtr;
- }
- _RecordsetPtr CDBOperation::CreateRecsetPtr()
- {
- HRESULT hr;
- _RecordsetPtr recsetPtr;
- hr = recsetPtr.CreateInstance(__uuidof( Command));
- if (FAILED(hr) ==TRUE)
- {
- return NULL;
- }
- return recsetPtr;
- }
方式二:OCCI
默認oracle安裝了occi庫,但是只是安裝了release版本的資源,因此需要將程序配置為release模式,或者是參看http://www.189works.com/article-42057-1.html為debug模式獲取必備的頭文件以及庫文件,本文采用的是release模式,使用默認安裝的庫文件以及頭文件。
1.修改配置屬性
改為Rlease模式
2.添加庫文件目錄
$(ORACLE_HOME)\oci\include
3.添加頭文件目錄
$(ORACLE_HOME)\oci\lib
4.添加庫文件:oraocci10.lib
應用程序:
點擊(此處)折疊或打開
- //代碼的目的就是驗證makefile中oracle的頭文件和lib文件路徑是否正確了
- #include <iostream>
- #define WIN32COMMON //避免函數重定義錯誤
- #include <occi.h>
- using namespace std;
- using namespace oracle::occi;
- int main()
- {
- Environment *env=Environment::createEnvironment();
- cout<<"success"<<endl;
- string name = "xxx";
- string pass = "xxx";
- string srvName = "xxx";
- try
- {
- Connection *conn = env->createConnection("bsm3", "bsm3", "BSM3");
- cout<<"conn success"<<endl;
- env->terminateConnection(conn);
- }
- catch(SQLException e)
- {
- cout<<e.what()<<endl;
- system("pause");
- return -1;
- }
- Environment::terminateEnvironment(env);
- cout<<"end!"<<endl;
- system("pause");
- return 0;
- }
服務器端:AIX服務器
方式一:OCCI
helloworld.cpp
點擊(此處)折疊或打開
- //代碼的目的就是驗證makefile中oracle的頭文件和lib文件路徑是否正確了
- #include <iostream>
- #include <occi.h>
- using namespace std;
- using namespace oracle::occi;
- main()
- {
- Environment *env=Environment::createEnvironment();
- cout<<"success"<<endl;
- string name = "xxx";
- string pass = "xxx";
- string srvName = "xxx";
- try
- {
- Connection *conn = env->createConnection(name, pass, srvName);
- cout<<"conn success"<<endl;
- env->terminateConnection(conn);
- }
- catch(SQLException e)
- {
- cout<<e.what()<<endl;
- }
- Environment::terminateEnvironment(env);
- cout<<"end!"<<endl;
- }
Makefile:
點擊(此處)折疊或打開
- ###########################################
- #Makefile for the OCCI demo programs
- ###########################################
- INC=-I${ORACLE_HOME}/precomp/public -I${ORACLE_HOME}/rdbms/public
- LIB=-L${ORACLE_HOME}/lib -locci #-bnoquiet #-bloadmap
- FLAGS=-q64 -g
- #為方便取下面三個變量,目標為helloworld,源文件是helloworld.cpp,編譯后文件helloworld.o
- PRG=helloworld
- SRC=helloworld.cpp
- OBJ=helloworld.o
- #下面是常規的makefile內容,$@表示依次取目標執行,這里只有helloworld一個目標。實際等價於
- #CC -o helloworld helloworld.o 不過加入了include和lib文件。而helloworld.o需要后續完成
- $(PRG):$(OBJ)
- @echo "begin link......"
- ${CC} ${FLAGS} ${INC} ${LIB} -o $@ $(OBJ)
- #helloworld目標依賴helloworld.o生成,所以該句就是編譯.c生成.o文件。只不過加入了include和lib文件
- $(OBJ):$(SRC)
- @echo "begin compile......"
- ${CC} ${FLAGS} ${INC} ${LIB} -c $(SRC)
- #后面的內容不是make的內容了,而是make clean內容。比如想重新make之前,清除.o等文件,執行make clean語句
- #.PRNOY語句表明 clean關鍵詞是個偽目標。make不自動執行。
- .PRONY:clean
- clean:
- @echo "Removing linked and compiled files....."
- rm -f $(OBJ) $(PRG)
