home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a063 / 9.img / SAMPLE / DBLIB / EXAMPLE8.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-03  |  5.1 KB  |  225 lines

  1. /*
  2. **    example8.c
  3. **
  4. ** This example illustrates how to use remote procedure calls
  5. ** and how to process return parameter values from stored
  6. ** procedures.  
  7. **
  8. ** The example uses the following stored procedure, 
  9. ** named "rpctest", which it assumes is located in the 
  10. ** user's default database.  Before running this example, 
  11. ** you must create "rpctest" in your default database.
  12. **
  13. **     create procedure rpctest
  14. **         (@param1 int out,
  15. **          @param2 int out,
  16. **          @param3 int out,
  17. **          @param4 int)
  18. **     as
  19. **     begin
  20. **         select "rpctest is running."
  21. **         select @param1 = 11
  22. **         select @param2 = 22
  23. **         select @param3 = 33
  24. **         select @param1
  25. **
  26. **         return 123
  27. **     end
  28. **
  29. */
  30.  
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <sybfront.h>
  35. #include <sybdb.h>
  36.  
  37. #define FMTSTR    "%-8.8s %-8.8s %-8.8s %-8.8s\n"
  38. #define FMTSTR1    "%-8.8s %-8.8s %8.8ld %8.8ld\n"
  39.  
  40. /* Function Prototypes */
  41. int             err_handler(DBPROCESS *, int, int, int, char *, char *);
  42. int             msg_handler(DBPROCESS *, DBINT, int, int, char *, char *, 
  43.                                         char *, DBUSMALLINT);
  44. int                 main();
  45.  
  46. main()
  47. {
  48.     LOGINREC         *login;
  49.     DBPROCESS        *dbproc;
  50.  
  51.     int              i;
  52.     int              numrets;
  53.     DBINT            param1 = 1;
  54.     DBINT            param2 = 2;
  55.     DBINT            param3 = 3;
  56.     DBINT            param4 = 4;
  57.     RETCODE          return_code;
  58.  
  59.     /* Initialize DB-Library. */
  60.     if (dbinit() == FAIL)
  61.         exit(ERREXIT);
  62.  
  63.     /* Install the user-supplied error-handling and message-handling
  64.      * routines. They are defined at the bottom of this source file.
  65.      */
  66.     dberrhandle(err_handler);
  67.     dbmsghandle(msg_handler);
  68.     
  69.     /* Allocate and initialize the LOGINREC structure to be used
  70.      * to open a connection to SQL Server.
  71.      */
  72.  
  73.     login = dblogin( );
  74.     DBSETLPWD(login, "server_password");
  75.     DBSETLAPP(login, "rpcexample");
  76.     
  77.     dbproc = dbopen(login, (char *)NULL);
  78.     dbuse(dbproc,"pubs");
  79.     /* Make the rpc. */
  80.     if (dbrpcinit(dbproc, "rpctest", (DBSMALLINT)0) == FAIL)
  81.     {
  82.         printf("dbrpcinit failed.\n");
  83.         dbexit();
  84.         exit(ERREXIT);
  85.     }
  86.  
  87.     if (dbrpcparam(dbproc, "@param1", (BYTE)DBRPCRETURN, SYBINT4, (DBINT)-1, 
  88.             (DBINT)-1, ¶m1) == FAIL)
  89.     {
  90.         printf("dbrpcparam failed.\n");
  91.         dbexit();
  92.         exit(ERREXIT);
  93.     }
  94.  
  95.     if (dbrpcparam(dbproc, "@param2", (BYTE)0, SYBINT4, (DBINT)-1, (DBINT)-1, 
  96.             ¶m2) == FAIL)
  97.     {
  98.         printf("dbrpcparam failed.\n");
  99.         dbexit();
  100.         exit(ERREXIT);
  101.     }
  102.  
  103.     if (dbrpcparam(dbproc, "@param3", (BYTE)DBRPCRETURN, SYBINT4, (DBINT)-1, 
  104.             (DBINT)-1, ¶m3) == FAIL)
  105.     {
  106.         printf("dbrpcparam failed.\n");
  107.         dbexit();
  108.         exit(ERREXIT);
  109.     }
  110.  
  111.     if (dbrpcparam(dbproc, "@param4", (BYTE)0, SYBINT4, (DBINT)-1, (DBINT)-1, 
  112.             ¶m4) == FAIL)
  113.     {
  114.         printf("dbrpcparam failed.\n");
  115.         dbexit();
  116.         exit(ERREXIT);
  117.     }
  118.  
  119.     if (dbrpcsend(dbproc) == FAIL)
  120.     {
  121.         printf("dbrpcsend failed.\n");
  122.         dbexit();
  123.         exit(ERREXIT);
  124.     }
  125.  
  126.     if (dbsqlok(dbproc) == FAIL)
  127.     {
  128.         printf("dbsqlok failed.\n");
  129.         dbexit();
  130.         exit(ERREXIT);
  131.     }
  132.  
  133.     while ((return_code = dbresults(dbproc)) != NO_MORE_RESULTS)
  134.     {
  135.         if (return_code == FAIL)
  136.         {
  137.             printf("dbresults failed.\n");
  138.             dbexit();
  139.             exit(ERREXIT);
  140.         }
  141.  
  142.         /* Print any rows that may have been returned. */
  143.         dbprrow(dbproc);
  144.  
  145.         /* Examine any return parameters that may have arrived. */
  146.  
  147.         numrets = dbnumrets(dbproc);
  148.         printf("%d return values received.\n\n", numrets);
  149.  
  150.         if (numrets > 0)
  151.         {
  152.             printf
  153.              (FMTSTR, "Name", "Type", "Length", "Value");
  154.             printf
  155.                 (FMTSTR,
  156.                  "------------", "------------",
  157.                  "------------", "------------");
  158.         }
  159.  
  160.         for (i = 1; i <= numrets; i++)
  161.         {
  162.             printf
  163.                 (FMTSTR1, dbretname(dbproc, i),
  164.                  dbprtype(dbrettype(dbproc, i)), dbretlen(dbproc, i),
  165.                  *((DBINT *)(dbretdata(dbproc, i))));
  166.         }
  167.  
  168.         if (dbhasretstat(dbproc))
  169.             printf("Return status = %ld\n", dbretstatus(dbproc));
  170.         else
  171.             printf("No return status for this command.\n");
  172.     }
  173.  
  174.     dbexit();
  175. }
  176.  
  177. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  178. DBPROCESS       *dbproc;
  179. int             severity;
  180. int             dberr;
  181. int             oserr;
  182. char            *dberrstr;
  183. char            *oserrstr;
  184. {
  185.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  186.         return(INT_EXIT);
  187.     else 
  188.     {
  189.         printf("DB-Library error:\n\t%s\n", dberrstr);
  190.  
  191.         if (oserr != DBNOERR)
  192.             printf("Operating-system error:\n\t%s\n", oserrstr);
  193.  
  194.         return(INT_CANCEL);
  195.     }
  196. }
  197.  
  198. int msg_handler(dbproc, msgno, msgstate, severity, msgtext, 
  199.                 srvname, procname, line)
  200.  
  201. DBPROCESS       *dbproc;
  202. DBINT           msgno;
  203. int             msgstate;
  204. int             severity;
  205. char            *msgtext;
  206. char            *srvname;
  207. char            *procname;
  208. DBUSMALLINT     line;
  209.  
  210. {
  211.     printf ("Msg %ld, Level %d, State %d\n", 
  212.             msgno, severity, msgstate);
  213.  
  214.     if (strlen(srvname) > 0)
  215.         printf ("Server '%s', ", srvname);
  216.     if (strlen(procname) > 0)
  217.         printf ("Procedure '%s', ", procname);
  218.     if (line > 0)
  219.         printf ("Line %d", line);
  220.  
  221.     printf("\n\t%s\n", msgtext);
  222.  
  223.     return(0);
  224. }
  225.