home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / sql / dblib / c / example8 / example8.c next >
Encoding:
C/C++ Source or Header  |  1996-04-03  |  5.0 KB  |  221 lines

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