- 论坛徽章:
- 0
|
oci连接问题,高手请进(急)
我刚连接成功是oci 7的 例:username@database
dabase是服务名在 net8 Assiant
给你我写的一个类
fundefin.h
/*
* $Header: cdemo6.h 14-jul-99.12:40:35 mjaeger Exp $
*/
/* Copyright (c) 1995, 1999, Oracle Corporation. All rights reserved.
*/
/*
NAME
cdemo6.h - header file for C++ Demo Program
MODIFIED (MM/DD/YY)
mjaeger 07/14/99 - bug 808870: OCCS: convert tabs, no long lines
slari 04/25/95 - merge changes from branch 1.1.720.1
slari 04/24/95 - Branch_for_patch
slari 04/21/95 - Creation
*/
extern "C"
{
#include <string.h>;
#include <oratypes.h>;
#include <ocidfn.h>;
#include <ocidem.h>;
}
/* oparse flags */
#define DEFER_PARSE 1 /* 语句延迟执行*/
#define NATIVE 1 /* */
#define VERSION_7 2 /* 版本号*/
#define NOTE_LEN 512 /*文件中记录的最大长度*/
/*字符串数组*/
typedef struct tagszArray
{
text Note[NOTE_LEN]; /*存放记录的字串*/
ub2 NoteLen; /*Note数组 的长度*/
sb2 Status; /*数组的特征码 */
ub2 Rc; /*执行后的返回码*/
}SZARRAY;
/*数字数组*/
typedef struct tagiArray
{
sword iNote; /*存放记录的字串*/
ub2 NoteLen; /*Note数组 的长度*/
ub2 Status; /*数组的特征码 */
ub2 Rc; /*执行后的返回码*/
}IARRAY;
/* Class forward declarations */
class connection;
class cursor;
/*
* This class represents a connection to ORACLE database.
*
* NOTE: This connection class is just given as an example and all possible
* operations on a connection have not been defined.
*/
class connection
{
friend class cursor;
public:
connection();/*初始化*/
~connection();
sword connect(const text *username, const text *password);
sword disconnect();
sword dbcof(); /*禁止自动提交*/
sword dbcon(); /*设置自动提交*/
sword dbcommit(); /*回滚*/
sword dbrollback(); /*提交*/
void display_error() const ;
private:
Lda_Def lda;
ub1 hda[HDA_SIZE];
enum conn_state
{
not_connected,
connected
};
conn_state state;
};
/*
* This class represents an ORACLE cursor.
*
* NOTE: This cursor class is just given as an example and all possible
* operations on a cursor have not been defined.
*/
class cursor
{
public:
cursor();
~cursor();
sword open(connection *conn_param);
sword close();
sword parse(const text *stmt);
/*绑定输入的数字参数*/
sword bindNumParam(sword num, ub1 *buf);
/*绑定输入的字符串数组参数*/
sword bindInStrArrayParam(text *StrNum,SZARRAY *szArray );
/*绑定输入的字符串参数*/
sword bindInStrParam(text *StrNum,ub1 *buf );
/*绑定输出字符串数组参数*/
sword bindOutStrArrayParam(sword pos, SZARRAY *szArray);
/*绑定输出字符串参数*/
sword bindOutStrParam(sword pos, ub1 *buf);
/*绑定输出数字参数*/
sword bindOutNumParam(sword pos, sword *buf);
/*绑定输出数字数组参数*/
sword bindOutNumArrayParam(sword pos, IARRAY *iArray);
/*描述提取出的列
sword describe(sword position, sb4 *dbsize, sb2 *dbtype, sb1 *cbuf,
sb4 *cbufl, sb4 *dsize, sb2 *prec, sb2 *scale, sb2 *nullok);
*/
/* 执行 sql 语句*/
sword execute();
/* 提取结果集*/
sword fetch(sword nRows);
sword get_error_code() const;
void display_error() const;
private:
//SZARRAY *szArray;
//IARRAY *iArray;
Cda_Def cda;
connection *conn;
enum cursor_state
{
not_opened,
opened
};
cursor_state state;
};
/*
* Error number macros
*/
#define CONERR_ALRCON -1 /* already connected */
#define CONERR_NOTCON -2 /* not connected */
#define CURERR_ALROPN -3 /* cursor is already open */
#define CURERR_NOTOPN -4 /* cursor is not opened */
//-------------------------------------------------------
下面是类funcbody.cpp
/*
* $Header: cdemo6.h 14-jul-99.12:40:35 mjaeger Exp $
*/
extern "C"
{
#include <stdio.h>;
#include <string.h>;
#include <oratypes.h>;
#include <ocidfn.h>;
#include <ocidem.h>;
#include <ociapr.h>;
}
#include "funcdefin.h"
/* oparse flags */
#define DEFER_PARSE 1 /* 语句延迟执行*/
#define NATIVE 1 /* */
#define VERSION_7 2 /* 版本号*/
#define MAX_D 2 /*数组的最大维数*/
#define ROW_ARRAY_SIZE 100 /*数组的行数*/
/*---------------------------------------------------------------------------------------------------
* 连接数据库,断开数据库,提交事务,回滚事务,对 lda 登陆数据区操作
* This class represents a connection to ORACLE database.
*
* NOTE: This connection class is just given as an example and all possible
* operations on a connection have not been defined.
------------------------------------------------------------------------------------------------------*/
connection::connection()
{
state = not_connected;
memset(hda,'\0', HDA_SIZE);
}
connection::~connection()
{
// disconnect if connection exists
if (state == connected)
{
if (disconnect())
{
display_error();
}
}
}
sword connection::connect(const text *username, const text *password)
{
sword status;
if (state == connected)
{
// this object is already connected
return (CONERR_ALRCON);
}
if ((status = olog(&lda, hda, (text *) username, -1,
(text *)password, -1, (text *) 0, -1,
OCI_LM_DEF)) == 0)
{
// successful login
state = connected;
printf("Connected to ORACLE as %s\n", username);
}
return (status);
}
sword connection::disconnect()
{
sword status;
if (state == not_connected)
{
// this object has not been connected
return (CONERR_NOTCON);
}
if ((status = ologof(&lda)) == 0)
{
// successful logout
state = not_connected;
}
return (status);
}
sword connection::dbcon()/*设置自动提交*/
{
if(ocon(&lda))
{
printf("set auto commit fail\n" ;
//错误码lda.rc
return -1;
}
return 0;
}
sword connection::dbcof()/*禁止自动提交*/
{
if(ocof(&lda))
{
printf("forbidden auto commit fail\n" ;
//错误码lda.rc
return -1;
}
return 0;
}
sword connection::dbcommit()/*提交事务*/
{
if(ocom(&lda))
{
printf("commit fail\n" ;
//lda.rc
return -1;
}
return 0;
}
sword connection::dbrollback()/*回滚事务*/
{
if(orol(&lda))
{
printf("rollback fail\n" ;
//lda.rc
return -1;
}
return 0;
}
/* write error message to the given file */
void connection::display_error() const
{
if (lda.rc != 0)
{
sword n;
text msg[512];
n = oerhms((cda_def *)&lda, lda.rc, msg, (sword) sizeof(msg));
//err_report(file, msg, lda.fc);
printf("错误信息=<%s>;,函数码=<%d>;\n",msg,lda.fc);
}
}
/*---------------------------------------------------------------------------------------------------
* cda 光标数据区操作
* This class represents an ORACLE cursor.
* NOTE: This cursor class is just given as an example and all possible
* operations on a cursor have not been defined.
------------------------------------------------------------------------------------------------------*/
cursor::cursor()
{
state = not_opened;
conn = (connection *)0;
}
cursor::~cursor()
{
if (state == opened)
{
if (close())
display_error();
}
}
/*-----------------------------
打开光标
------------------------------*/
sword cursor: pen(connection *conn_param)
{
sword status;
if (state == opened)
{
// this cursor has already been opened
return (CURERR_ALROPN);
}
if ((status = oopen(&cda, &conn_param->;lda, (text *)0, -1, -1,
(text *)0, -1)) == 0)
{
// successfull open
state = opened;
conn = conn_param;
}
return (status);
}
/*-----------------------------
关闭光标
------------------------------*/
sword cursor::close()
{
sword status;
if (state == not_opened)
{
// this cursor has not been opened
return (CURERR_NOTOPN);
}
if ((status = oclose(&cda)) == 0)
{
// successful cursor close
state = not_opened;
conn = (connection *)0;
}
return (status);
}
/*解析sql 串*/
sword cursor::parse(const text *stmt)
{
sword ErrCode=0;
if( ErrCode=oparse(&cda, (text *)stmt, (sb4)-1,DEFER_PARSE, (ub4) VERSION_7) )
{
return ErrCode;
}
return 0;
}
/*绑定输入的数字参数*/
sword cursor::bindNumParam(sword num, ub1 *buf)
{
sword ErrCode=0;
if(ErrCode=(obndrn(&cda, num, buf, sizeof(buf),INT_TYPE,-1,(sb2 *)0, (text *)0, -1, -1)) )
{
return ErrCode;
}
return 0;
}
/*绑定输入的字符串数组参数*/
sword cursor::bindInStrArrayParam(text *StrNum,SZARRAY *szArray)
{
sword ErrCode=0;
ub4 dim=2;
if(ErrCode=(obndra(&cda, (text *)StrNum,-1,
(ub1 *)szArray[0].Note,sizeof(szArray[0].Note),VARCHAR2_TYPE,
-1,&szArray[0].Status,&szArray[0].NoteLen,
&szArray[0].Rc,(ub4 )ROW_ARRAY_SIZE,&dim,
(text *)0,-1,-1) ))
{
return ErrCode;
}
return 0;
}
/*绑定输入的字符串参数*/
sword cursor::bindInStrParam(text *StrNum,ub1 *buf )
{
sword ErrCode=0;
if(ErrCode=(obndra(&cda, (text *)StrNum, -1,
(ub1 *)buf,sizeof(buf), VARCHAR2_TYPE,
-1,0,0,
0,0,0,
0,-1,-1) ))
{
return ErrCode;
}
return 0;
}
/*绑定输出字符串数组参数*/
sword cursor::bindOutStrArrayParam(sword pos, SZARRAY *szArray)
{
sword ErrCode=0;
if(ErrCode=(odefin(&cda, pos,(ub1 *)szArray[0].Note,NOTE_LEN,VARCHAR2_TYPE ,-1,(sb2 *)szArray[0].Status,
(text *)0, -1, -1,(ub2 *)szArray[0].NoteLen,(ub2 *)szArray[0].Rc)))
{
return ErrCode;
}
return 0;
}
/*绑定输出字符串参数*/
sword cursor::bindOutStrParam(sword pos, ub1 *buf)
{
sword ErrCode=0;
if(ErrCode=(odefin(&cda, pos,(ub1 *)buf,
sizeof(buf),VARCHAR2_TYPE ,-1,
(sb2 *)0,(text *)0, -1,
-1,(ub2 *)0,(ub2 *)0)))
{
return ErrCode;
}
return 0;
}
/*绑定输出数字参数*/
sword cursor::bindOutNumParam(sword pos, sword *buf)
{
sword ErrCode=0;
if(ErrCode=(odefin(&cda, pos,(ub1 *)buf,sizeof(buf),INT_TYPE,-1,(sb2 *)0,
(text *)0, -1, -1,(ub2 *)0,(ub2 *)0)))
{
return ErrCode;
}
return 0;
}
/*绑定输出数字数组参数*/
sword cursor::bindOutNumArrayParam(sword pos, IARRAY *iArray)
{
sword ErrCode=0;
if(ErrCode=(odefin(&cda, pos,(ub1 *)iArray[0].iNote,sizeof(iArray[0].iNote),INT_TYPE,-1,(sb2 *)iArray[0].Status,
(text *)0, -1, -1,&iArray[0].NoteLen,
&iArray[0].Rc)))
{
return ErrCode;
}
return 0;
}
/*执行 sql 语句 */
sword cursor::execute()
{
sword ErrCode=0;
if(oexec(&cda))
{
return ErrCode;
}
return 0;
}
/*fetch 提取的行将存入odefin 绑定的数组中 不需要 oexec() 函数*/
sword cursor::fetch(sword nRows)
{
sword ErrCode=0;
if(ofen(&cda,nRows))
{
return ErrCode;
}
return 0;
}
/* 取上一个函数的错误码 */
sword cursor::get_error_code() const
{
return (cda.rc);
}
/* write error message to the given file */
void cursor::display_error() const
{
if (cda.rc != 0)
{
sword ErrCode=0;
text msg[512];
ErrCode = oerhms(&conn->;lda, cda.rc, msg, (sword) sizeof(msg));
printf("错误信息=<%s>;,错误码=<%d>;\n",msg,cda.fc);
}
}
刚写的,如果好用的话跟我说一下printfscanf@163.com |
|