| OUCL访问Sybase数据库示例 |
#ifndef SYBASE_HPP
#define SYBASE_HPP
#include "owl.hpp"
#include "struct.hpp"
#include "jiaoyi.hpp"
#include <ctpublic.h>
extern TPrint *Print;
extern char PrintFileName[256];
extern CS_CONTEXT *g_context;
extern CS_CONNECTION *g_connection;
extern CS_CONTEXT *gr_context;
extern CS_CONNECTION *gr_connection;
int Trim(char *str);
//CS_RETCODE clientmsg_callback();
//CS_RETCODE servermsg_callback();
CS_RETCODE servermsg_callback(CS_CONTEXT *cp,CS_CONNECTION *chp,CS_SERVERMSG *msgp);
CS_RETCODE clientmsg_callback(CS_CONTEXT *cp,CS_CONNECTION *conn,CS_CLIENTMSG *msgp);
int ConnectLocalDatabase(char *,char *,char *);
int DisconnectLocalDatabase();
int ConnectRemoteDatabase(char *,char *,char *);
int DisconnectRemoteDatabase();
int ExecLocalSql(char *);
int ExecRemoteSql(char *);
int Exit_On_Fail(CS_CONTEXT *context, CS_RETCODE ret, char *str);
#endif
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctpublic.h>
#include "sybase.hpp"
CS_CONTEXT *g_context;
CS_CONNECTION *g_connection;
CS_CONTEXT *gr_context;
CS_CONNECTION *gr_connection;
int Trim(char *str)
{
char temp[512];
int n=0;
memset(temp,'\0',512);
for(int i=0;i<strlen(str);i++) {
if(str[i]!=' ') {
temp[n]=str[i];
n++;
}
if(n>510)
break;
}
temp[n]='\0';
strcpy(str,temp);
return 1;
}
CS_RETCODE servermsg_callback(CS_CONTEXT *cp,CS_CONNECTION *chp,CS_SERVERMSG *msgp)
{
return(CS_SUCCEED);
}
CS_RETCODE clientmsg_callback(CS_CONTEXT *cp,CS_CONNECTION *conn,CS_CLIENTMSG *msgp)
{
return(CS_SUCCEED);
}
int ConnectLocalDatabase(char *server,char *username,char *password)
{
CS_RETCODE ret;
g_context = (CS_CONTEXT *) NULL;
ret = cs_ctx_alloc(CS_VERSION_100, &g_context);
if(Exit_On_Fail(g_context,ret,"cs_ctx_alloc failed")==0)
return 0;
ret = ct_init(g_context, CS_VERSION_100);
if(Exit_On_Fail(g_context,ret,"ct_init failed")==0)
return 0;
ret = ct_callback(g_context,NULL,CS_SET,CS_CLIENTMSG_CB,(CS_VOID *)clientmsg_callback);
if(Exit_On_Fail(g_context,ret,"ct_callback for client message failed")==0)
return 0;
ret = ct_callback(g_context,NULL,CS_SET,CS_SERVERMSG_CB,(CS_VOID *)servermsg_callback);
if(Exit_On_Fail(g_context,ret,"ct_callback for server message failed")==0)
return 0;
ret = ct_con_alloc(g_context, &g_connection);
if(Exit_On_Fail(g_context,ret,"ct_con_alloc failed")==0)
return 0;
ret = ct_con_props(g_connection, CS_SET, CS_USERNAME,username,CS_NULLTERM,NULL);
if(Exit_On_Fail(g_context,ret,"Can not set user name")==0)
return 0;
ret = ct_con_props(g_connection, CS_SET, CS_PASSWORD,password, CS_NULLTERM, NULL);
if(Exit_On_Fail(g_context,ret,"Can not set password")==0)
return 0;
ret = ct_connect(g_connection, (CS_CHAR *)server, CS_NULLTERM);
if(Exit_On_Fail(g_context,ret,"Can not connect server")==0)
return 0;
if( ret != CS_SUCCEED )
return 0;
return 1;
}
int DisconnectLocalDatabase()
{
CS_RETCODE ret;
ret = ct_close(g_connection, CS_UNUSED);
if(Exit_On_Fail(g_context,ret,"ct_close failed")==0)
return 0;
ret = ct_con_drop(g_connection);
if(Exit_On_Fail(g_context,ret,"ct_con_drop failed")==0)
return 0;
ret = ct_exit(g_context, CS_UNUSED);
if(Exit_On_Fail(g_context,ret,"ct_exit failed")==0)
return 0;
ret = cs_ctx_drop(g_context);
if(Exit_On_Fail(g_context,ret,"cs_ctx_drop failed")==0)
return 0;
return 1;
}
int ConnectRemoteDatabase(char *server,char *username,char *password)
{
CS_RETCODE ret;
gr_context = (CS_CONTEXT *) NULL;
ret = cs_ctx_alloc(CS_VERSION_100, &gr_context);
if(Exit_On_Fail(gr_context,ret,"cs_ctx_alloc failed")==0)
return 0;
ret = ct_init(gr_context, CS_VERSION_100);
if(Exit_On_Fail(gr_context,ret,"ct_init failed")==0)
return 0;
ret = ct_callback(gr_context,NULL,CS_SET,CS_CLIENTMSG_CB,(CS_VOID *)clientmsg_callback);
if(Exit_On_Fail(gr_context,ret,"ct_callback for client message failed")==0)
return 0;
ret = ct_callback(gr_context,NULL,CS_SET,CS_SERVERMSG_CB,(CS_VOID *) servermsg_callback);
if(Exit_On_Fail(gr_context,ret,"ct_callback for server message failed")==0)
return 0;
ret = ct_con_alloc(gr_context, &gr_connection);
if(Exit_On_Fail(gr_context,ret,"ct_con_alloc failed")==0)
return 0;
ret = ct_con_props(gr_connection, CS_SET, CS_USERNAME,username,CS_NULLTERM,NULL);
if(Exit_On_Fail(gr_context,ret,"Can not set user name")==0)
return 0;
ret = ct_con_props(gr_connection, CS_SET, CS_PASSWORD,password, CS_NULLTERM, NULL);
if(Exit_On_Fail(gr_context,ret,"Can not set password")==0)
return 0;
ret = ct_connect(gr_connection, (CS_CHAR *)server, CS_NULLTERM);
if(Exit_On_Fail(gr_context,ret,"Can not connect server")==0)
return 0;
if( ret != CS_SUCCEED )
return 0;
return 1;
}
int DisconnectRemoteDatabase()
{
CS_RETCODE ret;
ret = ct_close(gr_connection, CS_UNUSED);
if(Exit_On_Fail(gr_context,ret,"ct_close failed")==0)
return 0;
ret = ct_con_drop(gr_connection);
if(Exit_On_Fail(gr_context,ret,"ct_con_drop failed")==0)
return 0;
ret = ct_exit(gr_context, CS_UNUSED);
if(Exit_On_Fail(gr_context,ret,"ct_exit failed")==0)
return 0;
ret = cs_ctx_drop(gr_context);
if(Exit_On_Fail(gr_context,ret,"cs_ctx_drop failed")==0)
return 0;
return 1;
}
int ExecLocalSql(char *sqlStr)
{
// char buffer[80];
CS_COMMAND *cmd;
// CS_DATAFMT datafmt[4];
CS_INT rows_read,result_type;
CS_RETCODE ret;
int ok = 0;
ret = ct_cmd_alloc(g_connection,&cmd);
if(Exit_On_Fail(g_context,ret,"ct_cmd_alloc failed")==0)
return 0;
ret = ct_command(cmd,CS_LANG_CMD,sqlStr,CS_NULLTERM,CS_UNUSED);
if(Exit_On_Fail(g_context,ret,"ct_command failed")==0)
return 0;
ret = ct_send(cmd);
if(Exit_On_Fail(g_context,ret,"ct_send failed")==0)
return 0;
while((ret=ct_results(cmd,&result_type)) == CS_SUCCEED) {
switch((int)result_type) {
case CS_CMD_SUCCEED:
ok = 1;
break;
case CS_CMD_FAIL:
ok = 0;
break;
case CS_CMD_DONE://lfr add here
//ok = 1;
break;
}//switch(res_type)
} //while
ret = ct_cmd_drop(cmd);
return ok;
}
int ExecRemoteSql(char *sqlStr)
{
// char buffer[80];
CS_COMMAND *cmd;
// CS_DATAFMT datafmt[4];
CS_INT rows_read,result_type;
CS_RETCODE ret;
int ok = 0;
ret = ct_cmd_alloc(gr_connection,&cmd);
if(Exit_On_Fail(gr_context,ret,"ct_cmd_alloc failed")==0)
return 0;
ret = ct_command(cmd,CS_LANG_CMD,sqlStr,CS_NULLTERM,CS_UNUSED);
if(Exit_On_Fail(gr_context,ret,"ct_command failed")==0)
return 0;
ret = ct_send(cmd);
if(Exit_On_Fail(gr_context,ret,"ct_send failed")==0)
return 0;
while((ret=ct_results(cmd,&result_type)) == CS_SUCCEED) {
switch((int)result_type) {
case CS_CMD_SUCCEED:
ok = 1;
break;
case CS_CMD_FAIL:
ok = 0;
break;
case CS_CMD_DONE://lfr add here
//ok = 1;
break;
}//switch(res_type)
} //while
ret = ct_cmd_drop(cmd);
return ok;
}
int Exit_On_Fail(CS_CONTEXT *context,CS_RETCODE ret,char *str)
{
if(ret!=CS_SUCCEED) {
MsgDialog("错误信息",str);
if(context!=(CS_CONTEXT *)NULL) {
ct_exit(context,CS_FORCE_EXIT);
cs_ctx_drop(context);
}
return 0;
}
return 1;
}
|
|
|
|