27,580
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sybfront.h>
#include <sybdb.h>
#include "sybdbex.h"
#define FMTSTR "%-8.8s %-8.8s %-8.8s %-8.8s\n"
#define FMTSTR1 "%-8.8s %-8.8s %8.8d %8.8s\n"
/* Forward reference
*/
void doexit(char *);
int
main(int argc, char *argv[])
{
LOGINREC *login;
DBPROCESS *dbproc;
int i;
int numrets;
DBINT param1 = 1;
DBINT param2 = 2;
DBINT param3 = 3;
DBINT param4 = 4;
RETCODE return_code;
printf("Demo of SQL queries in a command batch\n\n");
fflush(stdout);
/* Initialize DB-Library. */
if (dbinit() == FAIL)
{
exit(ERREXIT);
}
/*
** Install the user-supplied error-handling and message-handling
** routines. They are defined at the bottom of this source file.
*/
dberrhandle((EHANDLEFUNC)err_handler);
dbmsghandle((MHANDLEFUNC)msg_handler);
/*
** Allocate and initialize the LOGINREC structure to be used
** to open a connection to ASE.
*/
login = dblogin( );
DBSETLUSER(login, USER);
DBSETLPWD(login, PASSWORD);
DBSETLAPP(login, "rpcexample");
dbproc = dbopen(login, "demo");
/* Make the rpc. */
if (dbrpcinit(dbproc, "proc_getcardinfor", (DBSMALLINT)0) == FAIL)
{
doexit("dbrpcinit failed.\n");
}
if (dbrpcparam(dbproc, "@cardno", (BYTE)NULL, SYBCHAR, 19, 2,
(BYTE *)¶m1) == FAIL)
{
doexit("dbrpcparam 1 failed.\n");
}
if (dbrpcparam(dbproc, "@ret_code", (DBCHAR)DBRPCRETURN, SYBCHAR, 2, 2,
(BYTE *)¶m2) == FAIL)
{
doexit("dbrpcparam 2 failed.\n");
}
if (dbrpcparam(dbproc, "@compname", (BYTE)NULL, SYBVARCHAR, 10, strlen("@compname"),
(BYTE *)¶m3) == FAIL)
{
doexit("dbrpcparam 3 failed.\n");
}
if (dbrpcparam(dbproc, "@comptype", (DBCHAR)DBRPCRETURN, SYBCHAR, 2, 2,
(BYTE *)¶m4) == FAIL)
{
doexit("dbrpcparam 4 failed.\n");
}
if (dbrpcsend(dbproc) == FAIL)
{
doexit("dbrpcsend failed.\n");
}
if (dbsqlok(dbproc) == FAIL)
{
doexit("dbsqlok failed.\n");
}
else
{
printf("Query secussed.\n");
}
while ((return_code = dbresults(dbproc)) != NO_MORE_RESULTS)
{
if (return_code == FAIL)
{
doexit("dbresults failed.\n");
}
/* Print any rows that may have been returned. */
dbprrow(dbproc);
}
/* Examine any return parameters that may have arrived. */
numrets = dbnumrets(dbproc);
printf("%d return values received.\n\n", numrets);
if (numrets > 0)
{
printf(FMTSTR, "Name", "Type", "Length", "Value");
printf(FMTSTR,
"------------", "------------",
"------------", "------------");
}
for (i = 1; i <= numrets; i++)
{
printf(FMTSTR1, dbretname(dbproc, i), dbprtype(dbrettype(dbproc, i)),
dbretlen(dbproc, i), ((DBCHAR *)(dbretdata(dbproc, i))));
}
if (dbhasretstat(dbproc))
{
printf("Return status = %d\n", dbretstatus(dbproc));
}
else
{
printf("No return status for this command.\n");
}
dbexit();
exit(STDEXIT);
}
void
doexit(char *s1)
{
printf(s1);
dbexit(); /* always call dbexit before returning to OS */
exit(ERREXIT);
}
int CS_PUBLIC
err_handler(DBPROCESS *dbproc, int severity, int dberr, int oserr,
char *dberrstr, char *oserrstr)
{
if ((dbproc == NULL) || (DBDEAD(dbproc)))
{
return (INT_EXIT);
}
else
{
fprintf(ERR_CH, "DB-Library error:\n\t%s\n", dberrstr);
if (oserr != DBNOERR)
{
fprintf(ERR_CH, "Operating-system error:\n\t%s\n", oserrstr);
}
return (INT_CANCEL);
}
}
int CS_PUBLIC
msg_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate, int severity,
char *msgtext, char *srvname, char *procname, int line)
{
/*
** Ignore the 'Changed database to' and 'Changed language to'
** messages.
*/
if (msgno == 5701 || msgno == 5703)
{
return (0);
}
fprintf(ERR_CH, "Msg %d, Level %d, State %d\n",
msgno, severity, msgstate);
if (strlen(srvname) > 0)
{
fprintf(ERR_CH, "Server '%s', ", srvname);
}
if (strlen(procname) > 0)
{
fprintf(ERR_CH, "Procedure '%s', ", procname);
}
if (line > 0)
{
fprintf(ERR_CH, "Line %d", line);
}
fprintf(ERR_CH, "\n\t%s\n", msgtext);
return (0);
}
e...