《proc批量导入导出oracle数据库表》由会员分享,可在线阅读,更多相关《proc批量导入导出oracle数据库表(12页珍藏版)》请在金锄头文库上搜索。
1、最近在开发一个项目中,为了解决数据库瓶颈,不得不把数据库中的数据导出为文本文件。文本传到客户端后又要导入到数据库。本人用Builder嵌入PROC+写了一个导入导出的DLL。假如对你有用深感荣幸!具体内容如下:一、预备工作计算机环境:Win 2000 PRO,Oracle9i,C+ Builder 5.5引入必要的ORACLE内部函数:要用的函数在$(ORACEL_HOME)bin qlora9.dll链接库中。为了能在C+ Builder中使用,先得生成LIB:implibsqlora9.lib sqlora9.dll二、源文件分析/-/加入必要的头文件#include #include #
2、include #include #include#include #include #include #include #include/说明DLL的输出函数extern C _declspec(dlleXPort) int _stdcall ConnectDB(const char *Username, const char *PassWord, const char *Dbname);extern C _declspec(dllexport) int _stdcall ImportTxtfile(TList *LengthArray, String *FieldArray, const
3、char *TableName, const char *FileName);extern C _declspec(dllexport) int _stdcall ExportTxtfile(const char *Sql, const char *FileName);#pragma hdrstop/-#define MAX_ITEMS 20 /定义最大字段数#define MAX_VNAME_LEN 30 /定义选择表项最大长度#define MAX_INAME_LEN 30 /定义指示器变量名字的最大长度EXEC SQL INCLUDEsqlca; /说明SQL通讯区EXEC SQL IN
4、CLUDE oraca; /说明ORACLE通讯区EXEC SQL INCLUDE sqlda; /说明SQL语句描述结构/*SQLDA结构体请查相关资料*/EXEC ORACLE OPTION (ORACA = YES);EXEC ORACLE OPTION (RELEASE_CURSOR = YES);/说明ORACLE外部函数extern C _declspec(dllimport) void _stdcall sqlclu(SQLDA*);extern C _declspec(dllimport) void _stdcall sqlnul(short*, short*, int*);e
5、xtern C _declspec(dllimport) void _stdcall sqlprc(int*, int*, int*);extern C _declspec(dllimport) strUCt SQLDA * _stdcallsqlald(int, unsigned int, unsigned int);SQLDA *SelectUnit; /定义选择项描述SQLDA *BindUnit; /定义输入项空间/定义变量,以存放连接数据库的参数EXEC SQL BEGIN DECLARE SECTION; char User20;/用户名 char Pwd20;/密码 char D
6、B20;/数据库服务名EXEC SQL END DECLARE SECTION;bool bConnect = false;/是否连接标志#pragma hdrstop#pragma argsused/C+ Builder DLL的主函数BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved) return 1;/*- 连接数据库-*/int _stdcall ConnectDB(const char *Username, const char *Password, const char *Dbna
7、me) strcpy(User, Username); strcpy(Pwd, Password); strcpy(DB, Dbname); EXEC SQL CONNECT :User IDENTIFIED BY :Pwd USING :DB; if (sqlca.sqlcode N = MAX_ITEMS; for (int i=0; i Ii = (short *)malloc(sizeof(short *); BindUnit-Vi = (char *)malloc(MAX_VNAME_LEN); for (int i=0; i Ii = (short *)malloc(sizeof(
8、short *); SelectUnit-Vi = (char *)malloc(MAX_VNAME_LEN); EXEC SQL WHENEVER SQLERROR GOTOsqlerr;/DO sql_error(导出出错);/设置语句 EXEC SQL PREPARE SQLSA FROM :sqlstr; EXEC SQL DECLARE Cursorbase CURSOR FOR SQLSA; /输入描述处理 BindUnit-N = MAX_ITEMS; EXEC SQL DESCRIBE BIND VARIABLES for SQLSA INTO BindUnit; if (BindUnit-F N = BindUnit-F; /打开光标 EXEC SQL OPEN Cursorbase USING DESCRipTOR BindUnit; /选择项处理 EXEC SQL DESCRIBE SELECT LIST for SQLSA INTO S