答案:
最近在开发一个项目中,为了解决数据库IO瓶颈,不得不把数据库中的数据导出为文本文件。文本传到客户端后又要导入到数据库。本人用C++Builder嵌入PROC++写了一个导入导出的DLL。如果对你有用深感荣幸!详细内容如下:
一、准备工作
计算机环境:Win 2000 PRO,ORACLE 9i,C++ Builder 5.5
引入必要的ORACLE内部函数:要用的函数在$(ORACEL_HOME)\bin qlora9.dll链接库中。为了能在C++ Builder中使用,先得生成LIB:implib sqlora9.lib sqlora9.dll
二、源文件分析
//-------------------------------------------------------------------------
//加入必要的头文件
#include<vcl.h> #include<windows.h> #include<stdio.h> #include<stdlib.h> #include<string.h>
#include<time.h> #include<math.h> #include<fcntl.h> #include<io.h> #include<sys tat.h>
//说明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 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 INCLUDE sqlca; //说明SQL通讯区
EXEC SQL INCLUDE 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*);
extern "C" _declspec(dllimport) void _stdcall sqlprc(int*, int*, int*);
extern "C" _declspec(dllimport) struct SQLDA * _stdcall sqlald(int, unsigned int, unsigned int);
SQLDA *SelectUnit; //定义选择项描述
SQLDA *BindUnit; //定义输入项空间
//定义变量,以存放连接数据库的参数
EXEC SQL BEGIN DECLARE SECTION;
char User[20];//用户名
char Pwd[20];//密码
char DB[20];//数据库服务名
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 *Dbname)
{
strcpy(User, Username);
strcpy(Pwd, Password);
strcpy(DB, Dbname);
EXEC SQL CONNECT :User IDENTIFIED BY :Pwd USING :DB;
if (sqlca.sqlcode < 0)
return -1;
bConnect = true;
return 0;
}
/*---------------------------------------------------------------------------
导出文本函数
因为不确定SELECT语句的表及字段,所以我使用动态语句(ORACLE DYNAMIC SQL)的//第四种方式。动态SQL方法四是在不确定SQL语句的选择项与输入项,且不知个数与数据类型的情况下使用的一种复杂程序设计技术。
---------------------------------------------------------------------------*/
int _stdcall ExportTxtfile(const char *Sql/*SQL选择语句*/, const char FileName/*导出目标文本文件名*/)
{
int null_ok, precision, scale;
int handle;
if ((handle = open(FileName, O_CREAT|O_TEXT|O_APPEND|O_RDWR, S_IREAD|S_IWRITE)) == -1)
{
//文件打开出错
return -1;
}
//定义变量,以存放SQL语句
EXEC SQL BEGIN DECLARE SECTION;
char sqlstr[256];
EXEC SQL END DECLARE SECTION;
//检查是否连接数据库
if (bConnect == false) return -2;
strcpy(sqlstr/*.arr*/, Sql);
// sqlstr.len = strlen(sql);
//给描述区分配空间
if ((SelectUnit = sqlald(MAX_ITEMS, MAX_VNAME_LEN, MAX_INAME_LEN)) == (SQLDA *)NULL)
{
//空间分配失败
return -3;
}
if ((BindUnit = sqlald(MAX_ITEMS, MAX_VNAME_LEN, MAX_INAME_LEN)) == (SQLDA *)NULL)
{
//空间分配失败
return -3;
}
//给查询返回值存储区分配空间
SelectUnit->N = MAX_ITEMS;
for (int i=0; i < MAX_ITEMS; i++)
{
BindUnit->I[i] = (short *)malloc(sizeof(short *));
BindUnit->V[i] = (char *)malloc(MAX_VNAME_LEN);
}
for (int i=0; i < MAX_ITEMS; i++)
{
SelectUnit->I[i] = (short *)malloc(sizeof(short *));
SelectUnit->V[i] = (char *)malloc(MAX_VNAME_LEN);
}
EXEC SQL WHENEVER SQLERROR GOTO sqlerr;//DO sql_error("导出出错");
//设置SQL语句
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 < 0)
{
return -4;
//输入项过多
}
BindUnit->N = BindUnit->F;
//打开光标
EXEC SQL OPEN Cursorbase USING DESCRIPTOR BindUnit;
//选择项处理
EXEC SQL DESCRIBE SELECT LIST for SQLSA INTO SelectUnit;
if (SelectUnit->F < 0)
{
return -4;
//选择表项过多
}
SelectUnit->N = SelectUnit->F;
//因为所有格式,类型都是不确定的,所以要得到正确的
上一个:Oracle 9i Installation on Red Hat Linux
下一个:通过JDBC操纵Oracle数据库LOB字段的几种情况分析