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 / example5 / example5.c next >
Encoding:
C/C++ Source or Header  |  1996-04-03  |  3.6 KB  |  137 lines

  1. /*    example5.c */
  2. /*
  3. ** This example illustrates dbconvert.    It converts a
  4. ** number of constants to strings, a number of strings
  5. ** to numerical or binary quantities, and a number of
  6. ** numerical quantities to other numerical types.
  7. **
  8. */
  9.  
  10. #if defined(DBNTWIN32)
  11. #include <windows.h>
  12. #endif
  13.  
  14. #include <stdio.h>
  15. #include <sqlfront.h>
  16. #include <sqldb.h>
  17.  
  18. #define ARRAY_LEN    20
  19.  
  20. /* Forward declarations of the error handler and message handler.
  21. */
  22. int err_handler(DBPROCESS*, int, int, int, char*, char*);
  23. int msg_handler(DBPROCESS*, DBINT, int, int, char*);
  24.  
  25. main(argc, argv)
  26. int    argc;
  27. char    *argv[];
  28. {
  29.     /* These variables hold the results of data conversions. */
  30.     static DBBINARY    my_binary_array[ARRAY_LEN]
  31.     = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
  32.     DBFLT8    my_flt8;
  33.     DBINT    my_int4;
  34.     DBMONEY    my_money;
  35.     DBCHAR    my_string[ARRAY_LEN];
  36.  
  37.         dbinit();        /* initialize dblib */
  38.  
  39.     /* Install the user-supplied error-handling and message-handling
  40.     * functions. They are defined at the bottom of this source file.
  41.     */
  42.     dbmsghandle((DBMSGHANDLE_PROC)msg_handler);
  43.     dberrhandle((DBERRHANDLE_PROC)err_handler);
  44.  
  45.     /* Convert numerical and binary constants to strings. */
  46.  
  47.     dbconvert
  48.     ((DBPROCESS *)NULL, SQLBINARY, my_binary_array, (DBINT)8,
  49.             SQLCHAR, my_string,(DBINT)-1);
  50.     printf("Binary constant 0x123456789abcdef0 converted to string");
  51.     printf("\"%s\".\n\n", my_string);
  52.  
  53.     my_flt8 = 55.555;
  54.     dbconvert
  55.     ((DBPROCESS *)NULL, SQLFLT8, (BYTE *) &my_flt8,
  56.             (DBINT)-1, SQLCHAR, my_string, (DBINT)-1);
  57.     printf
  58.     ("Floating-pt constant 55.555 converted to string \"%s\".\n\n",
  59.     my_string);
  60.  
  61.     /* Convert string constants to numerical and binary quantities. */
  62.  
  63.     dbconvert
  64.     ((DBPROCESS *)NULL, SQLCHAR, "123", (DBINT)-1, SQLINT4, (BYTE *)
  65.         &my_int4, (DBINT)-1);
  66.     printf
  67.     ("String constant \"123\" converted to 4-byte integer %ld.\n\n",
  68.     my_int4);
  69.  
  70.     dbconvert
  71.     ((DBPROCESS *)NULL,
  72.     SQLCHAR, "0xfedc", (DBINT)-1, SQLBINARY, my_binary_array,
  73.             (DBINT)ARRAY_LEN);
  74.     printf("String constant \"0xfedc\" converted to binary sequence ");
  75.     printf("%hx.\n\n", *((DBSMALLINT *)my_binary_array));
  76.  
  77.     dbconvert
  78.     ((DBPROCESS *)NULL, SQLCHAR, "123.456", (DBINT)-1, SQLFLT8,
  79.        (BYTE *) &my_flt8, (DBINT)-1);
  80.     printf("String constant \"123.456\" converted to ");
  81.     printf("floating-pt number %f.\n\n", my_flt8);
  82.  
  83.     /* Convert numerical types to other numerical types. */
  84.  
  85.     my_flt8 = 98.76;
  86.     dbconvert
  87.        ((DBPROCESS *)NULL, SQLFLT8, (BYTE *) &my_flt8, (DBINT)-1, SQLMONEY,
  88.         (BYTE *) &my_money, (DBINT)-1);
  89.     dbconvert
  90.        ((DBPROCESS *)NULL, SQLMONEY, (BYTE *) &my_money, (DBINT)-1, SQLCHAR,
  91.            my_string, (DBINT)-1);
  92.     printf
  93.     ("floating-pt number %f converted to money value %s.\n\n",
  94.     my_flt8, my_string);
  95.  
  96.     dbconvert
  97.        ((DBPROCESS *)NULL, SQLMONEY, (BYTE *) &my_money, (DBINT)-1, SQLFLT8,
  98.            (BYTE *) &my_flt8, (DBINT)-1);
  99.     printf
  100.     ("money value %s converted to floating-pt value %f.\n\n",
  101.     my_string, my_flt8);
  102.  
  103.     return(STDEXIT);
  104. }
  105.  
  106. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  107. DBPROCESS    *dbproc;
  108. int    severity;
  109. int    dberr;
  110. int    oserr;
  111. char    *dberrstr;
  112. char    *oserrstr;
  113. {
  114.     printf("DB-LIBRARY error:\n\t%s\n", dberrstr);
  115.  
  116.     if (oserr != DBNOERR)
  117.         printf("Operating-system error:\n\t%s\n", oserrstr);
  118.  
  119.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  120.         return(INT_EXIT);
  121.  
  122.     return(INT_CANCEL);
  123. }
  124.  
  125. int msg_handler(dbproc, msgno, msgstate, severity, msgtext)
  126. DBPROCESS    *dbproc;
  127. DBINT    msgno;
  128. int    msgstate;
  129. int    severity;
  130. char    *msgtext;
  131. {
  132.     printf
  133.     ("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
  134.     msgno, msgstate, severity, msgtext);
  135.     return(0);
  136. }
  137.