DM API和OCI编程方式比较
2008-09-03 12:50:56 来源:WEB开发网b) OCI
达梦OCI的连接跟API流程差不多,只是申请的句柄不一样。主要也可分为四步:第一,申请环境句柄;第二,在环境句柄上申请错误句柄;第三,在环境句柄上申请上下文句柄;第四,调用函数建立连接。请看下面示例:
#include"oci.h"
#include
#include
#include
OCIEnv*envhp;//环境句柄
OCIError*errhp;//错误句柄
OCISvcCtx*svchp;//上下文句柄
OCIStmt*stmthp=(OCIStmt*)NULL;//语句句柄
swordinit_handles(OCIEnv**envhp,OCISvcCtx**svchp,OCIError**errhp);
swordfinish_demo(booleanloggedon,OCIEnv*envhp,OCISvcCtx*svchp,OCIError*errhp,OCIStmt*stmthp,text*userid);
swordlog_on(OCIEnv*envhp,OCIError*errhp,OCISvcCtx*svchp,text*uid,text*pwd,text*cstring);
voidreport_error(OCIError*errhp);
intmain()
{
text*cstring=(text*)"127.0.0.1";//注意,如果你配置了dm_oci.conf文件(如:MyServer=(127.0.0.1)),那么你也可以在这里指定一个服务名MyServer
text*username=(text*)"SYSDBA";
text*password=(text*)"SYSDBA";
intlogged_on=FALSE;
//初始化各种句柄
if(init_handles(&envhp,&svchp,&errhp))
{
(void)printf("FAILED:init_handles()n");
returnfinish_demo(logged_on,envhp,svchp,errhp,stmthp,username);
}
//登录到数据库服务
if(log_on(envhp,errhp,svchp,username,password,cstring))
{
(void)printf("FAILED:log_on()n");
returnfinish_demo(logged_on,envhp,svchp,errhp,stmthp,username);
}
logged_on=TRUE;
//
//在这里做SQL动作
//
//登出数据库服务,并清理各种句柄
returnfinish_demo(logged_on,envhp,svchp,errhp,stmthp,username);
}
swordinit_handles(OCIEnv**envhp,OCISvcCtx**svchp,OCIError**errhp)
{
(void)printf("Environmentsetup....n");
/*初始化OCI应用环境*/
if(OCIInitialize(OCI_DEFAULT,(dvoid*)0,
(dvoid*(*)(dvoid*,size_t))0,
(dvoid*(*)(dvoid*,dvoid*,size_t))0,
(void(*)(dvoid*,dvoid*))0))
{
(void)printf("FAILED:OCIInitialize()n");
returnOCI_ERROR;
}
/*初始化环境句柄*/
if(OCIEnvInit((OCIEnv**)envhp,(ub4)OCI_DEFAULT,
(size_t)0,(dvoid**)0))
{
(void)printf("FAILED:OCIEnvInit()n");
returnOCI_ERROR;
}
/*从环境句柄上分配一个上下文句柄*/
if(OCIHandleAlloc((dvoid*)*envhp,(dvoid**)svchp,
(ub4)OCI_HTYPE_SVCCTX,(size_t)0,(dvoid**)0))
{
(void)printf("FAILED:OCIHandleAlloc()onsvchpn");
returnOCI_ERROR;
}
/*从环境句柄上分配一个错误信息句柄*/
if(OCIHandleAlloc((dvoid*)*envhp,(dvoid**)errhp,
(ub4)OCI_HTYPE_ERROR,(size_t)0,(dvoid**)0))
{
(void)printf("FAILED:OCIHandleAlloc()onerrhpn");
returnOCI_ERROR;
}
returnOCI_SUCCESS;
}
/*-----------------------------------------------------------------*/
/*使用给定的用户名和口令登录到指定的数据库服务上*/
/*-----------------------------------------------------------------*/
swordlog_on(OCIEnv*envhp,OCIError*errhp,OCISvcCtx*svchp,text*uid,text*pwd,text*cstring)
{
(void)printf("Loggingonas%s....n",uid);
/*SettheauthenticationhandleintheServicehandle*/
if(OCILogon(envhp,errhp,&svchp,uid,strlen((char*)uid),
pwd,strlen((char*)pwd),cstring,strlen((char*)cstring)))
{
(void)printf("FAILED:OCILogon()n");
returnOCI_ERROR;
}
(void)printf("%sloggedon.n",uid);
returnOCI_SUCCESS;
}
/*-----------------------------------------------------------------*/
/*释放申请的各种句柄*/
/*-----------------------------------------------------------------*/
voidfree_handles(OCIEnv*envhp,OCISvcCtx*svchp,OCIError*errhp,OCIStmt*stmthp)
{
(void)printf("Freeinghandles...n");
if(svchp)
(void)OCIHandleFree((dvoid*)svchp,(ub4)OCI_HTYPE_SVCCTX);
if(errhp)
(void)OCIHandleFree((dvoid*)errhp,(ub4)OCI_HTYPE_ERROR);
if(stmthp)
(void)OCIHandleFree((dvoid*)stmthp,(ub4)OCI_HTYPE_STMT);
if(envhp)
(void)OCIHandleFree((dvoid*)envhp,(ub4)OCI_HTYPE_ENV);
return;
}
/*---------------------------------------------------------------------*/
/*断开连接,并释放各种句柄*/
/*---------------------------------------------------------------------*/
swordfinish_demo(booleanloggedon,OCIEnv*envhp,OCISvcCtx*svchp,OCIError*errhp,OCIStmt*stmthp,text*userid)
{
if(loggedon)
OCILogoff(svchp,errhp);
free_handles(envhp,svchp,errhp,stmthp);
returnOCI_SUCCESS;
}
voidreport_error(OCIError*errhp)
{
textmsgbuf[512];
sb4errcode=0;
(void)OCIErrorGet((dvoid*)errhp,(ub4)1,(text*)NULL,&errcode,
msgbuf,(ub4)sizeof(msgbuf),(ub4)OCI_HTYPE_ERROR);
(void)printf("ERRORCODE=%d",errcode);
(void)printf("%.*sn",512,msgbuf);
return;
}
- ››DM API和OCI编程方式比较
- ››API之菜单函数
- ››API之控件与消息函数
- ››API之进程和线程函数
- ››API之打印函数
- ››API之位图、图标和光栅运算函数
- ››API之网络函数
- ››API之设备场景函数
- ››API之消息函数
- ››API之文本和字体函数
- ››API文件产生器-javadoc.exe
- ››API解读:Thread
更多精彩
赞助商链接