- 論壇徽章:
- 0
|
我想很多人在VC下連接Oracle數(shù)據(jù)庫時(shí),一般使用ODBC或ADO控件,而使用Proc*C/C++應(yīng)該會(huì)比較少。并且我在網(wǎng)上搜索了一下,關(guān)于這方面的介紹也不多。本人也是因工作需要而使用它,在對此一無所知的情況下,十分感謝“時(shí)代朝陽數(shù)據(jù)庫技術(shù)中心”:
http://www.xiaotong-db.com.cn/kfgj/oracle/kfgj/O8I_proc_jc_vc.htm
給了我啟發(fā)。現(xiàn)在把本人的經(jīng)驗(yàn),貼出來讓大家分享(以下是圍繞著產(chǎn)生cpp文件來說明的)。
1、設(shè)置目錄:
打開“tools->;Options”,轉(zhuǎn)到“Directories”頁,在"Show Directories for"中
1)選“Executable Files”,在列表中加入proc.exe的目錄(ORACLE_HOME\bin):
如:D:\oracle\ora81\bin
2)選“Include Files”,在列表中加入proc*c/c++包含頭文件的目錄(ORACLE_HOME\PRECOMP\PUBLIC):
如:D:\ORACLE\ORA81\PRECOMP\PUBLIC
3)選“Library Files”,在列表中加入proc*c/c++ Lib所在的目錄(ORACLE_HOME\PRECOMP\LIB\MSVC):
如:D:\ORACLE\ORA81\PRECOMP\LIB\MSVC
*注:我的oracle客戶端程序是安裝在D:\oracle\ora81下;
2、建立一MFC工程(這步在此不作介紹);
3、往工程中加入,Proc*c/c++的源和頭文件(這步在此也不作介紹),例加入oratools.pc、oratools.h;
oratools.h
- #ifndef __LZP_ORACLE_TOOLS_HEAD__
- #define __LZP_ORACLE_TOOLS_HEAD__
- class CLzpOracle
- {
- public:
- CLzpOracle();
- ~CLzpOracle();
- int ConnectDatabase(char *strConn);
- };
- #endif
復(fù)制代碼
oratools.pc
- #include "stdafx.h"
- #include "oratools.h"
- /*SQL通訊區(qū)說明*/
- EXEC SQL INCLUDE SQLCA;
- /*SQL錯(cuò)誤處理說明語句*/
- EXEC SQL WHENEVER SQLERROR CONTINUE;
- EXEC SQL WHENEVER NOTFOUND CONTINUE;
- CLzpOracle::CLzpOracle()
- {
- }
- CLzpOracle::~CLzpOracle()
- {
- }
- int CLzpOracle::ConnectDatabase(char *strConn)
- {
- EXEC SQL BEGIN DECLARE SECTION;
- char usrpwd[60];
- EXEC SQL END DECLARE SECTION;
- strcpy(usrpwd,strConn);
- EXEC SQL CONNECT :usrpwd;
- if (sqlca.sqlcode < 0)
- {
- return -1;
- }
- return 0;
- }
復(fù)制代碼
4、右擊新加入的proc*c/c++文件(如oratools.pc),選擇"settings..."到"project settings",然后Custom Build頁:
在Commands下寫入命令:proc 輸入入文件名 oname=輸出文件名(如:proc oratools.pc oname=oratools.cpp) 注如果要生成.cpp文件,oname=這項(xiàng)一定要填,不然它會(huì)自動(dòng)生成.c文件
在Outputs下寫入輸出文件名(如oratools.cpp),這項(xiàng)我剛開始以為不寫上面的oname=,會(huì)輸出這里填寫的文件名,但事實(shí)上并不是這樣;
5、然后編譯工程,它將會(huì)自動(dòng)預(yù)編譯產(chǎn)生c/c++文件(如oratols.cpp),然后把這個(gè)文件作如下修改:
1)把#include "stdafx.h"提到最前面;
2)把函數(shù)的定義用下面的括起
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
- #ifdef __cplusplus
- extern "C" {
- #endif
- /* SQLLIB Prototypes */
- extern void sqlcxt (void **, unsigned long *,
- struct sqlexd *, const struct sqlcxp *);
- extern void sqlcx2t(void **, unsigned long *,
- struct sqlexd *, const struct sqlcxp *);
- extern void sqlbuft(void **, char *);
- extern void sqlgs2t(void **, char *);
- extern void sqlorat(void **, unsigned long *, void *);
- /* Forms Interface */
- static const int IAPSUCC = 0;
- static const int IAPFAIL = 1403;
- static const int IAPFTL = 535;
- extern void sqliem(char *, int *);
- #ifdef __cplusplus
- }
- #endif
復(fù)制代碼
6、把lib文件加入工程中:
project->;settings->;link->;object/library modules中加入oraSQL8.lib.
7、最后編譯鏈接產(chǎn)生可執(zhí)行文件。注:如果重編譯整個(gè)工程時(shí),如果是產(chǎn)生.cpp文件一定別忘了修改預(yù)編譯產(chǎn)生的文件。 |
|