1. 程式人生 > >利用oracle官網提供的occi庫在windows下操作oracle資料庫

利用oracle官網提供的occi庫在windows下操作oracle資料庫

1、先前用occi操作oracle資料庫都是失敗,後來發現是我下載的庫版本和對應的dll版本不一致導致:如32庫, 卻下載了64位的dll,因此導致在初始化環境就失敗,百思不得其解。

2、在下載好對應的32位庫和32位dll,可以正常初始化環境,操作oracle資料庫。

3、只需下載2個檔案,其他版本的檔案根據自己需要下載。如我下載的是:instantclient-basic-nt-11.2.0.3.0.zip 和 instantclient-sdk-nt-11.2.0.4.0.zip

4、我下載的檔案只支援vs2005和vs2008

5、發現執行正常,無需在安裝或配置oracle資料庫驅動等。

6、執行時依賴的庫有:針對我這次執行例子:oci.dll、oraocci11.dll、oraociei11.dll

7、dll可以在instantclient-basic-nt-11.2.0.3.0.zip 找到,

8、程式依賴的標頭檔案、庫檔案可以在instantclient-sdk-nt-11.2.0.4.0.zip找到

9、依賴的標頭檔案位於instantclient-sdk-nt-11.2.0.4.0.zip資料夾中的include資料夾

10、依賴的庫檔案位於instantclient-sdk-nt-11.2.0.4.0.zip資料夾中的msvc資料夾,只用2個庫檔案:oci.lib 和 oraocci11.lib

附上測試程式碼:


#include <iostream>
#define WIN32COMMON //避免函式重定義錯誤
#include <occi.h>
using namespace std;
using namespace oracle::occi;


int main()
{
	//建立OCCI上下文環境
	Environment *env = Environment::createEnvironment();
	if (NULL == env) {  
            printf("createEnvironment error.\n");  
            return -1;  
        }
	else
		cout << "success" << endl;

	string name = "使用者名稱";
	string pass = "密碼";
	string srvName = "資料庫地址/資料庫名";//資料庫地址與資料庫名之間用“/”分隔

	try
	{	
		//建立資料庫連線
		Connection *conn = env->createConnection(name, pass, srvName);//使用者名稱,密碼,資料庫名
		 if(NULL == conn) {  
                 printf("createConnection error.\n");  
                 return -1;  
                }
		else{    
                cout << "conn success" << endl;
 
 
               } 

          //關閉連線
	  env->terminateConnection(conn);          
	}
	catch (SQLException e)
       {
		cout << e.what() << endl;
		system("pause");
		return -1;
	}


       // 釋放OCCI上下文環境  
	Environment::terminateEnvironment(env);
	cout << "end!" << endl;
	system("pause");
	return 0;
}