免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3320 | 回复: 2
打印 上一主题 下一主题

C语言怎么调用一个存储过程,并取那个回值 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-11 00:00 |只看该作者 |倒序浏览
存值储过程写好了,用C怎么调用,那个回值怎么才能取到,各位高手们,我在线等了

论坛徽章:
0
2 [报告]
发表于 2008-01-11 00:52 |只看该作者
有人帮帮我忙吗?我这儿用dblibC,sybase下也没有example的目录,能帮我的速度帮我一下

论坛徽章:
0
3 [报告]
发表于 2008-01-11 21:53 |只看该作者
唉,真是一言难尽啊!看你可怜兮兮的,我就简单的复制个例子给你:
/*
** Confidential property of Sybase, Inc.
** (c) Copyright Sybase, Inc. 1992 to ???
** All rights reserved.
*/

/*
** %M%:      %I%      %G%      %U%
**  
**  
**  
*/
#if USE_SCCSID
static char Sccsid[] = {"%Z% %M% %I% %G%"};
#endif /* USE_SCCSID */

/*
**        example8.c
**
** This example illustrates how to use remote procedure calls
** and how to process return parameter values from stored
** procedures.  
**
** The example uses the following stored procedure,
** named "rpctest", which it assumes is located in the
** user's default database.  Before running this example,
** you must create "rpctest" in your default database.
**
**     create procedure rpctest
**         (@param1 int out,
**          @param2 int out,
**          @param3 int out,
**          @param4 int)
**     as
**     begin
**         select "rpctest is running."
**         select @param1 = 11
**         select @param2 = 22
**         select @param3 = 33
**         select @param1
**
**         return 123
**     end
**
*/


#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.8ld %8.8ld\n"

/* Forward reference
*/
void doexit PROTOTYPE((char *));

main()
{
        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 SQL Server.
         */

        login = dblogin( );
        DBSETLUSER(login, USER);
        DBSETLPWD(login, PASSWORD);
        DBSETLAPP(login, "rpcexample");
       
        dbproc = dbopen(login, (char *)NULL);

        /* Make the rpc. */
        if (dbrpcinit(dbproc, "rpctest", (DBSMALLINT)0) == FAIL)
                doexit("dbrpcinit failed.\n");

        if (dbrpcparam
               (dbproc, "@param1", (BYTE)DBRPCRETURN, SYBINT4, -1, -1, (BYTE *)&param1)
           == FAIL)
                doexit("dbrpcparam 1 failed.\n");

        if (dbrpcparam(dbproc, "@param2", (BYTE)0, SYBINT4, -1, -1, (BYTE *)&param2)
           == FAIL)
                doexit("dbrpcparam 2 failed.\n");

        if (dbrpcparam
               (dbproc, "@param3", (BYTE)DBRPCRETURN, SYBINT4, -1, -1, (BYTE *)&param3)       
           == FAIL)
                doexit("dbrpcparam 3 failed.\n");

        if (dbrpcparam(dbproc, "@param4", (BYTE)0, SYBINT4, -1, -1, (BYTE *)&param4)
           == FAIL)
                doexit("dbrpcparam 4 failed.\n");

        if (dbrpcsend(dbproc) == FAIL)
                doexit("dbrpcsend failed.\n");

        if (dbsqlok(dbproc) == FAIL)
                doexit("dbsqlok failed.\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),
                         *((DBINT *)(dbretdata(dbproc, i))));
        }

        if (dbhasretstat(dbproc))
                printf("Return status = %ld\n", dbretstatus(dbproc));
        else
                printf("No return status for this command.\n");

        dbexit();
}


void doexit(s1)
char *s1;
{
        printf(s1);
    dbexit();            /* always call dbexit before returning to OS */
    exit(ERREXIT);
}


int CS_PUBLIC err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
DBPROCESS    *dbproc;
int          severity;
int          dberr;
int          oserr;
char         *dberrstr;
char         *oserrstr;
{
    if ((dbproc == (DBPROCESS *)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(dbproc, msgno, msgstate, severity, msgtext,
                srvname, procname, line)

DBPROCESS       *dbproc;
DBINT           msgno;
int             msgstate;
int             severity;
char            *msgtext;
char            *srvname;
char            *procname;
int            line;

{
        fprintf (ERR_CH, "Msg %ld, 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);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP