home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / odbcsdk / samples / admndemo / errcheck.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-07  |  4.6 KB  |  137 lines

  1. //*---------------------------------------------------------------------------------
  2. //|  ODBC System Administrator
  3. //|
  4. //|  This code is furnished on an as-is basis as part of the ODBC SDK and is
  5. //|  intended for example purposes only.
  6. //|
  7. //*---------------------------------------------------------------------------------
  8. #include "errcheck.h"
  9. #include "standard.h"
  10. #include "strings.h"
  11.  
  12.  
  13. //*---------------------------------------------------------------------------------
  14. //|    Global variables
  15. //*---------------------------------------------------------------------------------
  16. char             szErrOut[100];
  17.  
  18. dCSEG(char)    szErrTitle[]                        =    "Error!";
  19. dCSEG(char) szError[]                            =    "Error: %s,  File: %s, Line: %d";
  20. dCSEG(char) szOutOfMemory[]                    =    "Memory levels are very low.  Please exit other applications and try your request again.";
  21. dCSEG(char) szInvalidParms[]                    =    "Invalid parameters";
  22. dCSEG(char) szRegisterClassFailed[]            =    "Register class failed";
  23.  
  24.  
  25. //*------------------------------------------------------------------------
  26. //| GetSQLState:
  27. //|     Parameters:
  28. //|            henv                - Pointer to environment
  29. //|            hdbc                - Pointer to connection handle
  30. //|            hstmt                - Pointer to statement
  31. //|            szState            - Return sqlstate
  32. //|            szNative            - Native return code (driver specific)
  33. //|            szMessage        - Return message
  34. //*------------------------------------------------------------------------
  35. LPSTR GetSQLState(HENV henv, HDBC hdbc, HSTMT hstmt, 
  36.                 LPSTR szState, SDWORD FAR * pfNative, LPSTR szMessage)
  37. {
  38.     RETCODE     rc;
  39.     SWORD        cb;
  40.  
  41.     rc = SQLError(henv, hdbc, hstmt, szState, pfNative,
  42.                 szMessage, RTN_MSG_SIZE, &cb);
  43.     if(rc == SQL_NO_DATA_FOUND || rc == SQL_ERROR)
  44.         return NULL;
  45.     else
  46.         return szState;
  47. }
  48.  
  49.  
  50.  
  51. //*------------------------------------------------------------------------
  52. //| DoPostError:
  53. //|    This function will post an error message to standard output, whereever
  54. //|        that should be.
  55. //| Parms:
  56. //|    in            szErr                        Error message
  57. //|    in            szFile                    File name
  58. //|    in            cbLine                    Line number
  59. //| Returns:
  60. //|    Nothing.
  61. //*---------------------------------------------------------------------------------
  62. void DoPostError(LPSTR szErr, LPSTR szFile, int cbLine)
  63. {
  64.     wsprintf(szErrOut, szError, (LPSTR)szErr, szFile, cbLine);
  65.     MessageBox(NULL, szErrOut, szErrTitle, MB_OK);
  66. }
  67.  
  68.  
  69.  
  70. //*------------------------------------------------------------------------
  71. //| PrintErrors:
  72. //|     Print out all relevant errors.
  73. //|            ci                -  Pointer to client information
  74. //*------------------------------------------------------------------------
  75. void PrintErrors(CHILDINFO FAR * ci)
  76. {
  77.     if(!ci->hwndOut)
  78.         DisplayErrors(ci->hwndOut, (LPSTR)szErrTitle, ci->henv, ci->hdbc, ci->hstmt);
  79.     PrintErrorsHwnd(ci->hwndOut, ci->henv, ci->hdbc, ci->hstmt);
  80. }
  81.  
  82.  
  83. //*------------------------------------------------------------------------
  84. //| PrintErrorsHwnd:
  85. //|    Does the actual work.  Needed as separate function for those
  86. //|    function which are not woking directly with a ci struct.
  87. //| Parms:
  88. //|    hwnd            Output window
  89. //|    henv            Environment handle
  90. //|    hdbc            Connection handle
  91. //|    hstmt            Statement handle
  92. //*------------------------------------------------------------------------
  93. void PrintErrorsHwnd(HWND hwnd, HENV henv, HDBC hdbc, HSTMT hstmt)
  94. {
  95.     char         szState[7]="";
  96.     char         szMessage[RTN_MSG_SIZE];
  97.     SDWORD     pfNative=0;
  98.  
  99.     while(GetSQLState(henv, hdbc, hstmt, 
  100.                 szState, &pfNative, szMessage) != NULL)
  101.         szWrite(hwnd, 
  102.                 GetidsString(idsErrorString, szErrOut, sizeof(szErrOut)), 
  103.                 (LPSTR)szState,
  104.                 (LPSTR)szMessage);
  105. }
  106.  
  107.  
  108. //*------------------------------------------------------------------------
  109. //| DisplayErrors:
  110. //|    This will take all of the errors from the ODBC handles and display
  111. //|    them using message box.  This is usually done when there is no
  112. //|    output window to write them to.
  113. //| Parms:
  114. //|    hwnd            Window handle to own the message box
  115. //|    title            The title for the message box
  116. //|    henv            Environment handle to look on
  117. //|    hdbc            Connection handle to look at
  118. //|    hstmt            Statement handle to look at
  119. //| Returns:
  120. //|    Nothing
  121. //*------------------------------------------------------------------------
  122. void DisplayErrors(HWND hwnd, LPSTR title, HENV henv, HDBC hdbc, HSTMT hstmt)
  123. {
  124.     char         szState[7]="";
  125.     char         szMessage[RTN_MSG_SIZE];
  126.     SDWORD     pfNative=0;
  127.  
  128.     while(GetSQLState(henv, hdbc, hstmt, 
  129.                 szState, &pfNative, szMessage) != NULL)
  130.         szMessageBox((hwnd) ? hwnd : GetActiveWindow(), 
  131.                         MB_ICONEXCLAMATION,
  132.                         title,
  133.                         GetidsString(idsMsgErrorString, szErrOut, sizeof(szErrOut)), 
  134.                         (LPSTR)szState,
  135.                         (LPSTR)szMessage);
  136. }
  137.