⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example.c

📁 当前不少单位在进行系统改造升级时
💻 C
字号:
/*example.c*/
#include "stdio.h"
#include "string.h"
#include "database_op.h"




int err_handler(DBPROCESS* dbproc,int severity,int dberr,int oserr,char* dberrstr,char* oserrstr)
{
	if (dbproc == NULL)
	{
		printf("!网络数据连接断开!");
		return(INT_EXIT);
	}
	else if (DBDEAD(dbproc))
	{
		printf("!数据连接断开!");
		return(INT_EXIT);
	}
	else
	{
		char tempstr[1024];
		sprintf(tempstr,"DB-LIBRARY error:%ld\t%s",dberr,dberrstr);
		printf(tempstr);
		if (oserr != DBNOERR)
			sprintf(tempstr,"Operating-system error:%ld\t%s",oserr,oserrstr);
		printf(tempstr);
		return(INT_CANCEL);
	}
}
int msg_handler(DBPROCESS* dbproc,DBINT msgno,int msgstate,int severity,char* msgtext, 
                char* srvname,char* procname,int line)
{
	char tempstr[1024];
	sprintf(tempstr,"SQL Server message %ld,state %d,severity %d:\t%s",&
		msgno,msgstate,severity,msgtext);
	/* comm_writelog(tempstr); */
	return(0);
}	



int f_db_disconnect(void* dbpoint)
{
	dbexit();
	return 0;
}

int f_db_connect(DBCHAR *user,DBCHAR *passwd,DBCHAR *server,DBPROCESS **ppdbproc)
{
  LOGINREC  *login_ptr;
  dbinit();
  
  dberrhandle(err_handler);/*装入用户提供的错误处理程序应用*/
  dbmsghandle(msg_handler);
	
  login_ptr=dblogin();

  DBSETLUSER(login_ptr,user);
  DBSETLPWD(login_ptr,passwd);
  *ppdbproc=dbopen(login_ptr,server);
  dbloginfree(login_ptr);

  if ((*ppdbproc) == (DBPROCESS *)NULL)
  {
    printf("Could not connect to Server!\n");
    return(FAIL);
  }
  return(SUCCEED);
}






int main()
{
	DBPROCESS *dbproc_2;
	DBPROCESS *dbproc;
	
	int ret ,i;
	char sqlcmd[1024],sql_tmp[1024];
	char usrname[32],password[32],servername[32];

	DBCHAR rowbuf[5][30];
	
	sqlcmd[0]='\0';
	sql_tmp[0]='\0';

/*test code */	
	sprintf(usrname,"ems");
	sprintf(password,"ems");
	sprintf(servername,"10.126.11.152");

	printf("connect datebase usrname=%s,password=%s,servername=%s!! ",usrname,password,servername);
	if(f_db_connect(usrname,password,servername,&dbproc)==FAIL) 
	{
		printf("connect datebase FAIL usrname=%s,password=%s,servername=%s!! ",usrname,password,servername);
		return -2;		
	}
	else printf("\nConnect OK!\n");
	
	if(f_db_connect(usrname,password,servername,&dbproc_2)==FAIL) 
	{
		printf("connect (2) datebase FAIL usrname=%s,password=%s,servername=%s!! ",usrname,password,servername);
		return -2;		
	}
	else printf("\nConnect OK!\n");
	

	sprintf(sql_tmp,"select * from dual");
	strcpy(sqlcmd,sql_tmp);
	dbcmd(dbproc,sqlcmd);
	ret=dbsqlexec(dbproc);
	if (ret==FAIL) 
	{
		printf("ERROR exec");
		return(-1);
	}
	else printf("\ndbsqlexec 1 OK!\n");

	dbcmd(dbproc_2,sqlcmd);
	ret=dbsqlexec(dbproc_2);
	if (ret==FAIL) 
	{
		printf("ERROR exec dbproc_2");
		return(-1);
	}
	else printf("\ndbsqlexec 2 OK!\n");
	
	for(i=0;i<dbnumcols(dbproc);i++)
		dbbind(dbproc,i+1,NTBSTRINGBIND,0,(BYTE *)rowbuf[i]);	
	
	for(i=0;i<dbnumcols(dbproc_2);i++)
		dbbind(dbproc_2,i+1,NTBSTRINGBIND,0,(BYTE *)rowbuf[i]);	

	
	printf("\nFirst:\n");
	
	printf("\nFrom dbproc : 1 :");
	if( (ret=dbnextrow(dbproc))!=NO_MORE_ROWS )
	{
		for(i=0;i<dbnumcols(dbproc);i++)
			printf("\n---%s---\n",rowbuf[i]);
	}
	else printf("\nFail!!!!\n");

	printf("\nFrom dbproc : 2 :");
	if( (ret=dbnextrow(dbproc_2))!=NO_MORE_ROWS )
	{
		for(i=0;i<dbnumcols(dbproc_2);i++)
			printf("\n---%s---\n",rowbuf[i]);
	}
	else printf("\nFail!!!!\n");
	
printf("\nSecond:\n");

	printf("\nFrom dbproc : 1 :");
	if( (ret=dbnextrow(dbproc))!=NO_MORE_ROWS )
	{
		for(i=0;i<dbnumcols(dbproc);i++)
			printf("\n---%s---\n",rowbuf[i]);
	}
	else printf("\nFail!!!!\n");
	
	printf("\nFrom dbproc : 2 :");
	if( (ret=dbnextrow(dbproc_2))!=NO_MORE_ROWS )
	{
		for(i=0;i<dbnumcols(dbproc_2);i++)
			printf("\n---%s---\n",rowbuf[i]);
	}
	else printf("\nFail!!!!\n");

	f_db_disconnect(dbproc);
	f_db_disconnect(dbproc_2);
	printf("!!!!!!!!!!!!!!end!!!!!!!!!!!!!!");	
	return 0 ;
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -