home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / esqlc / esql / samples.c / edblib.sqc < prev    next >
Encoding:
Text File  |  1995-12-29  |  29.1 KB  |  981 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  FILE: edblib.sqc
  4. //
  5. //      Mixed Embedded SQL for C and DB-Library program, based on 
  6. //      the "GenWin" Generic Embedded SQL for C Win16/Win32 program
  7. //
  8. //  FUNCTIONS:
  9. //
  10. //      WinMain() - Main program and message loop
  11. //      MainWndProc() - Main window procedure
  12. //      ConnectToSQLServer() - Connect to SQL Server using CONNECT TO
  13. //      QuerySQLServer() - Query a table in SQL Server
  14. //      ConnectDlgProc() - Connect box dialog procedure
  15. //      QueryDlgProc() - Query box dialog procedure
  16. //      AboutDlgProc() - About box dialog procedure
  17. //      ErrorHandler() - Embedded SQL for C error handler
  18. //      DblibErrorHandler() - DB-Library error handler
  19. //      DblibMessageHandler() - DB-Library SQL Server message handler
  20. //
  21. //  COMMENTS:
  22. //
  23. //      Copyright (C) 1992 - 1994 Microsoft Corporation
  24. //
  25. //////////////////////////////////////////////////////////////////////////////
  26.  
  27. #if defined (_WIN32)                // Win32
  28. #define DBNTWIN32
  29. #else                               // Win16
  30. #define DBMSWIN
  31. #endif
  32.  
  33. #define STRICT
  34. #define WIN32_LEAN_AND_MEAN  //excludes unused windows.h headers
  35. #include <windows.h>                // standard Windows include file
  36. #include <windowsx.h>               // extended Windows include file
  37. #include <sqlfront.h>               // DB-Library header
  38. #include <sqldb.h>                  // DB-Library header
  39. #include "gwutil.h"                 // utility header
  40. #include "edblib.h"                 // specific to this program
  41.  
  42. // GLOBAL VARIABLES
  43. char szAppName[] = "EmbeddedDBLib"; // application name
  44. HINSTANCE hinstMain;                // current instance
  45. HWND hwndMain;                      // main window handle
  46. HWND hwndEdit;                      // edit control handle
  47.  
  48. char szLogin[SQLID_MAX+1] = "";     // SQL Server login ID
  49. char szPassword[SQLID_MAX+1] = "";  // SQL Server password
  50. char szServer[SQLID_MAX+1] = "";    // SQL Server name
  51. char szDatabase[SQLID_MAX+1] = "";  // SQL Server database to use
  52. BOOL bConnected = FALSE;            // are we connected to SQL Server?
  53.  
  54. EXEC SQL BEGIN DECLARE SECTION;
  55. char szLastName[50] = "";           // author's last name
  56. DBPROCESS *pDbproc = NULL;
  57. EXEC SQL END DECLARE SECTION;
  58.  
  59. //////////////////////////////////////////////////////////////////////////////
  60. //
  61. //  FUNCTION: WinMain()
  62. //
  63. //      Creates class and window, message processing loop
  64. //
  65. //  PARAMETERS:
  66. //
  67. //      hinst - handle of current instance
  68. //      hinstPrev - handle of previous instance
  69. //      lpstrCmdLine - command line string
  70. //      nCmdShow - show window type
  71. //
  72. //  RETURNS: the value from PostQuitMessage()
  73. //
  74. //  COMMENTS:
  75. //
  76. //////////////////////////////////////////////////////////////////////////////
  77.  
  78. int WINAPI WinMain (
  79.     HINSTANCE hinst,
  80.     HINSTANCE hinstPrev,
  81.     LPSTR lpstrCmdLine,
  82.     int nCmdShow)
  83. {
  84.     MSG msg;                    // message
  85.     WNDCLASS wcMain;            // windows class
  86.     HFONT hfontFixed;           // fixed font
  87.     char chOpt;                 // gotten option character
  88.     char* pszParam;             // gotten parameter
  89.     char szScratch[200] = "";   // scratch area for wsprintf()
  90.  
  91.     BOOL bLogin = FALSE,        // bools for tracking options
  92.         bPassword = FALSE,
  93.         bServer = FALSE,
  94.         bDatabase = FALSE;
  95.  
  96.     // install Embedded SQL for C error handler
  97.     EXEC SQL WHENEVER SqlError CALL ErrorHandler();
  98.     // set Embedded SQL for C options
  99.     EXEC SQL SET OPTION LOGINTIME 10;
  100.     EXEC SQL SET OPTION QUERYTIME 100;
  101.     
  102.     // Save the instance handle in global variable
  103.     hinstMain = hinst;
  104.  
  105.     if (!hinstPrev)
  106.     {
  107.         // Fill in window class structure
  108.         wcMain.style = 0;                       // class style(s)
  109.         wcMain.lpfnWndProc = (WNDPROC) MainWndProc;             // window procedure
  110.         wcMain.cbClsExtra = 0;                  // no per-class extra data
  111.         wcMain.cbWndExtra = 0;                  // no per-window extra data
  112.         wcMain.hInstance = hinst;               // app that owns this class
  113.         wcMain.hIcon = LoadIcon(hinst, "AppIcon");             // icon from .RC file
  114.         wcMain.hCursor = LoadCursor(NULL, IDC_ARROW);          // arrow cursor
  115.         wcMain.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);    // white background
  116.         wcMain.lpszMenuName = "AppMenu";        // menu from .RC file
  117.         wcMain.lpszClassName = szAppName;       // name used in call to CreateWindow
  118.  
  119.         // Register the window class
  120.         if (!RegisterClass(&wcMain))
  121.             return (FALSE);
  122.     }
  123.  
  124.     // Create a main window for this application instance
  125.     hwndMain = CreateWindow(szAppName,  // class name
  126.         "Embedded SQL and DB-Library",  // window name
  127.         WS_OVERLAPPEDWINDOW,            // window style(s)
  128.         CW_USEDEFAULT,                  // init horizontal position
  129.         CW_USEDEFAULT,                  // init vertical position
  130.         CW_USEDEFAULT,                  // init width
  131.         CW_USEDEFAULT,                  // init height
  132.         NULL,                           // parent window handle
  133.         NULL,                           // menu or child window ID
  134.         hinst,                          // instance
  135.         NULL);                          // used for MDI windows only
  136.  
  137.     // If main window could not be created, return failure
  138.     if (!hwndMain)
  139.         return (FALSE);
  140.  
  141.     // Create edit window
  142.     hwndEdit = CreateWindow("edit",
  143.         NULL,
  144.         WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
  145.             ES_LEFT | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
  146.         0, 0, 0, 0,         // initial dimensions all 0
  147.         hwndMain,           // parent handle
  148.         (HMENU)IDE_EDIT,    // unique child-window identifier
  149.         hinst,              // instance  handle
  150.         NULL);
  151.  
  152.     // If edit window could not be created, return failure
  153.     if (!hwndEdit)
  154.         return (FALSE);
  155.         
  156.     // get fixed font for edit control
  157.     hfontFixed = GetStockObject(ANSI_FIXED_FONT);
  158.     SendMessage(hwndEdit,
  159.         WM_SETFONT,
  160.         (WPARAM)hfontFixed,
  161.         FALSE);
  162.  
  163.     SetWindowText(hwndEdit, "");
  164.     
  165.     // display logo
  166.     PrintLine(hwndEdit,"Embedded SQL for C and DB-Library Win16/Win32 application");
  167.     
  168.     // get options and parameters
  169.     while (TRUE)
  170.     {
  171.         // all these command-line options are valid
  172.         //     {/s|/S} [sql_server]
  173.         //     {/u|/U} login_id
  174.         //     {/p|/P} [password]
  175.         //     {/d|/D} [database]
  176.  
  177.         chOpt = GetOption(__argc, __argv, "u:U:p:P:s:S:d:D:", &pszParam);
  178.         if (chOpt > 1)
  179.         {
  180.             // chOpt is valid argument
  181.             switch (chOpt)
  182.             {
  183.             case 'u':   // login ID
  184.             case 'U':
  185.                 bLogin = TRUE;
  186.                 if (pszParam != NULL)
  187.                 {
  188.                     lstrcpy(szLogin, pszParam);
  189.                 }
  190.                 break;
  191.             case 'p':   // password
  192.             case 'P':
  193.                 bPassword = TRUE;
  194.                 if (pszParam != NULL)
  195.                 {
  196.                     lstrcpy(szPassword, pszParam);
  197.                 }
  198.                 break;
  199.             case 's':   // SQL Server
  200.             case 'S':
  201.                 bServer = TRUE;
  202.                 if (pszParam != NULL)
  203.                 {
  204.                     lstrcpy(szServer, pszParam);
  205.                 }
  206.                 break;
  207.             case 'd':   // database
  208.             case 'D':
  209.                 bDatabase = TRUE;
  210.                 if (pszParam != NULL)
  211.                 {
  212.                     lstrcpy(szDatabase, pszParam);
  213.                 }
  214.                 break;
  215.             }
  216.  
  217.         }
  218.         if (chOpt == 0)
  219.         {
  220.             // end of argument list
  221.             break;
  222.         }
  223.         if ((chOpt == 1) || (chOpt == -1))
  224.         {
  225.             // standalone param or error
  226.             wsprintf(szScratch, "ERROR: Argument '%s' not recognized", (LPSTR)pszParam);
  227.             PrintLine(hwndEdit, szScratch);
  228.             break;
  229.         }
  230.     }
  231.  
  232.     if ((chOpt == 1) || (chOpt == -1))
  233.     {
  234.         // error
  235.     }
  236.  
  237.     ShowWindow(hwndMain, nCmdShow);     // Show the window
  238.     UpdateWindow(hwndMain);             // Sends WM_PAINT message
  239.  
  240.     if (bServer && bLogin && bPassword && bDatabase)
  241.     {
  242.         // all command-line options specified, so attempt connection
  243.         PostMessage(hwndMain, WM_CONNECTTOSQLSERVER, 0, 0);
  244.     }
  245.     else
  246.     {
  247.         // prompt for unspecified options
  248.         Post_WM_COMMAND(hwndMain,
  249.             IDM_FILE_CONNECT,
  250.             0,
  251.             0);
  252.     }
  253.  
  254.     // Acquire and dispatch messages until a WM_QUIT message is received
  255.     while (GetMessage(&msg,         // message structure
  256.         NULL,                       // handle of window receiving the message
  257.         0,                          // lowest message to examine
  258.         0))                         // highest message to examine
  259.     {
  260.         TranslateMessage(&msg);     // translate virtual key codes
  261.         DispatchMessage(&msg);      // dispatch message to window
  262.     }
  263.     return (msg.wParam);        // Returns the value from PostQuitMessage
  264. }
  265.  
  266. //////////////////////////////////////////////////////////////////////////////
  267. //
  268. //  FUNCTION: MainWndProc()
  269. //
  270. //      Main window procedure, processes messages
  271. //
  272. //  MESSAGES:
  273. //
  274. //      WM_COMMAND              application menu command
  275. //      WM_CREATE               create window
  276. //      WM_DESTROY              destroy window
  277. //      WM_SETFOCUS             window got focus
  278. //      WM_SIZE                 window has been resized
  279. //      WM_DESTROY              window is being destroyed
  280. //      WM_CONNECTTOSQLSERVER   connect to SQL Server
  281. //      WM_QUERYSQLSERVER       query a table in SQL Server
  282. //
  283. //  COMMENTS:
  284. //
  285. //////////////////////////////////////////////////////////////////////////////
  286.  
  287. LRESULT CALLBACK MainWndProc (
  288.     HWND hwnd,
  289.     UINT msg,
  290.     WPARAM wParam,
  291.     LPARAM lParam)
  292. {
  293.     WORD cmdNotify, idItem;
  294.     HWND hwndControl;
  295.     DLGPROC lpDlgProc;
  296.     int nWidth, nHeight;    
  297.  
  298.     switch (msg)
  299.     {
  300.     case WM_COMMAND:    // message: command from application menu
  301.         
  302.         cmdNotify = GET_WM_COMMAND_NOTIFY(wParam, lParam);
  303.         idItem = GET_WM_COMMAND_ID(wParam, lParam);
  304.         hwndControl = GET_WM_COMMAND_HWND(wParam, lParam);
  305.  
  306.         switch (idItem)
  307.         {
  308.         case IDM_FILE_CONNECT:
  309.             lpDlgProc = (DLGPROC) MakeProcInstance((FARPROC)ConnectDlgProc, hinstMain);
  310.  
  311.             DialogBox(hinstMain,
  312.                 "ConnectBox",
  313.                 hwnd,
  314.                 lpDlgProc);
  315.  
  316.             FreeProcInstance((FARPROC)lpDlgProc);
  317.             break;
  318.  
  319.         case IDM_FILE_QUERY:
  320.             if (bConnected)
  321.             {
  322.                 lpDlgProc = (DLGPROC) MakeProcInstance((FARPROC)QueryDlgProc, hinstMain);
  323.  
  324.                 DialogBox(hinstMain,
  325.                     "QueryBox",
  326.                     hwnd,
  327.                     lpDlgProc);
  328.  
  329.                 FreeProcInstance((FARPROC)lpDlgProc);
  330.             }
  331.             else
  332.             {
  333.                 PrintLine(hwndEdit, "ERROR: Not connected to SQL Server");
  334.             }
  335.             break;
  336.  
  337.         case IDM_FILE_EXIT:
  338.             DestroyWindow(hwnd);
  339.             break;
  340.  
  341.         case IDM_HELP_ABOUT:
  342.             lpDlgProc = (DLGPROC) MakeProcInstance((FARPROC)AboutDlgProc, hinstMain);
  343.  
  344.             DialogBox(hinstMain,
  345.                 "AboutBox",
  346.                 hwnd,
  347.                 lpDlgProc);
  348.  
  349.             FreeProcInstance((FARPROC)lpDlgProc);
  350.             break;
  351.  
  352.         default:
  353.             // pass it on if unproccessed
  354.             return (DefWindowProc(hwnd, msg, wParam, lParam));
  355.             break;
  356.  
  357.         }
  358.         break; // case WM_COMMAND
  359.  
  360.     case WM_SETFOCUS:   // message: window focus
  361.         SetFocus(hwndEdit);
  362.         break; // case WM_SETFOCUS
  363.  
  364.     case WM_SIZE:       // message: window focus
  365.         nWidth = LOWORD(lParam);
  366.         nHeight = HIWORD(lParam);
  367.         MoveWindow(hwndEdit,    // window to move
  368.             0,                  // x coord
  369.             0,                  // y coord
  370.             nWidth,             // width
  371.             nHeight,            // height
  372.             TRUE);              // repaint
  373.         break; // case WM_SIZE
  374.  
  375.     case WM_DESTROY:    // message: window being destroyed
  376.         EXEC SQL DISCONNECT ALL;
  377.         PostQuitMessage (0);
  378.         break; // case WM_DESTORY
  379.  
  380.     case WM_CONNECTTOSQLSERVER:     // private message: connect to SQL Server
  381.         ConnectToSQLServer(szServer,
  382.             szLogin,
  383.             szPassword,
  384.             szDatabase);
  385.         break; // case WM_CONNECTTOSQLSERVER
  386.  
  387.     case WM_QUERYSQLSERVER:     // private message: query SQL Server
  388.         QuerySQLServer();
  389.         break; // case WM_CONNECTTOSQLSERVER
  390.  
  391.     default:
  392.         // pass it on if unproccessed
  393.         return (DefWindowProc(hwnd, msg, wParam, lParam));
  394.         break;
  395.     }
  396.     return (0);
  397. }
  398.  
  399. ///////////////////////////////////////////////////////////////////////////////
  400. //
  401. //  FUNCTION: ConnectToSQLServer()
  402. //
  403. //      Establish connection to SQL Server
  404. //
  405. //  PARAMETERS:
  406. //
  407. //      szServer - connect to this SQL Server 
  408. //      szLogin - login ID
  409. //      szPassword - password
  410. //      szDatabase - database to use
  411. //
  412. //  RETURNS: 0
  413. //
  414. //  COMMENTS:
  415. //
  416. ///////////////////////////////////////////////////////////////////////////////
  417.  
  418. BOOL ConnectToSQLServer (
  419.     LPSTR szServer,
  420.     LPSTR szLogin,
  421.     LPSTR szPassword,
  422.     LPSTR szDatabase)
  423. {
  424.     EXEC SQL BEGIN DECLARE SECTION;
  425.     // for CONNECT TO statement
  426.     char szConnectToServerDatabase[(SQLID_MAX * 2)+2] = "";
  427.     char szConnectToLoginPassword[(SQLID_MAX * 2)+2] = "";
  428.     EXEC SQL END DECLARE SECTION;
  429.  
  430.     // set defaults
  431.     if (lstrlen(szDatabase) == 0)
  432.     {
  433.         lstrcpy(szDatabase, "pubs");
  434.     }
  435.     
  436.     PrintLine(hwndEdit, "Using:");
  437.     PrintStr(hwndEdit, "    SQL Server: ");
  438.     PrintLine(hwndEdit, szServer);
  439.     PrintStr(hwndEdit, "    Database: ");
  440.     PrintLine(hwndEdit, szDatabase);
  441.     PrintStr(hwndEdit, "    Login ID: ");
  442.     PrintLine(hwndEdit, szLogin);
  443.     PrintStr(hwndEdit, "    Password: ");
  444.     PrintLine(hwndEdit, szPassword);
  445.  
  446.     // build host variables for CONNECT TO
  447.     if (lstrlen(szServer) != 0)
  448.     {
  449.         lstrcat(szConnectToServerDatabase, szServer);
  450.         lstrcat(szConnectToServerDatabase, ".");
  451.     }
  452.     if (lstrlen(szDatabase) != 0)
  453.     {
  454.         lstrcat(szConnectToServerDatabase, szDatabase);
  455.     }
  456.  
  457.     if (lstrlen(szLogin) != 0)
  458.     {
  459.         lstrcat(szConnectToLoginPassword, szLogin);
  460.     }
  461.     if (lstrlen(szPassword) != 0)
  462.     {
  463.         lstrcat(szConnectToLoginPassword, ".");
  464.         lstrcat(szConnectToLoginPassword, szPassword);
  465.     }
  466.  
  467.     // attempt connection to SQL Server
  468.     EXEC SQL CONNECT TO :szConnectToServerDatabase
  469.         AS dblib_connection
  470.         USER :szConnectToLoginPassword;
  471.  
  472.     PrintStr(hwndEdit, "Connection to SQL Server '");
  473.     PrintStr(hwndEdit, szServer);
  474.     if (SQLCODE == 0)
  475.     {
  476.         bConnected = TRUE;
  477.         PrintLine(hwndEdit, "' established");
  478.  
  479.         EXEC SQL GET CONNECTION dblib_connection INTO :pDbproc;
  480.         if (pDbproc != NULL)
  481.         {
  482.             PrintLine(hwndEdit, "Got DBPROCESS connection");
  483.         }
  484.         else
  485.         {
  486.             PrintLine(hwndEdit, "ERROR: Getting DBPROCESS connection");
  487.         }
  488.  
  489.         PrintLine(hwndEdit, "From the File menu choose Query to execute a query");
  490.  
  491.         return (TRUE);
  492.     }
  493.     else
  494.     {
  495.         // problem connecting to SQL Server
  496.         bConnected = FALSE;
  497.         PrintLine(hwndEdit, "' failed");
  498.         return (FALSE);
  499.     }
  500. }
  501.  
  502. ///////////////////////////////////////////////////////////////////////////////
  503. //
  504. //  FUNCTION: QuerySQLServer()
  505. //
  506. //      Query SQL Server
  507. //
  508. //  PARAMETERS: none
  509. //
  510. //  RETURNS: 0
  511. //
  512. //  COMMENTS:
  513. //
  514. ///////////////////////////////////////////////////////////////////////////////
  515.  
  516. BOOL QuerySQLServer ()
  517. {
  518.     // Pointers to error handlers
  519.     DBERRHANDLE_PROC lpfnOldErrorHandler;
  520.     // Pointers to message handlers
  521.     DBMSGHANDLE_PROC lpfnOldMessageHandler;
  522.     
  523. #ifndef _WIN32
  524.     DBERRHANDLE_PROC lpfnDblibErrorHandler;
  525.     DBMSGHANDLE_PROC lpfnDblibMessageHandler;
  526. #endif
  527.  
  528.     char szSQLCommand[200] = "";        // string for holding SQL command
  529.     RETCODE r;                          // DB-Library RETCODE
  530.     STATUS s;                           // DB-Library STATUS for dbnextrow()
  531.     BOOL bSuccess = TRUE;               // did this function succeed?
  532.     
  533.     EXEC SQL BEGIN DECLARE SECTION;
  534.     char szFirstName[50] = "";
  535.     EXEC SQL END DECLARE SECTION;
  536.  
  537.     // display what user specified
  538.     PrintStr(hwndEdit, "Author's last name: ");
  539.     PrintLine(hwndEdit, szLastName);
  540.  
  541.     // query the authors table
  542.     EXEC SQL SELECT au_fname INTO :szFirstName
  543.         from authors where au_lname = :szLastName;
  544.  
  545.     if (SQLCODE == 0)
  546.     {
  547.         PrintStr(hwndEdit, "That author's first name is: ");
  548.         PrintStr(hwndEdit, szFirstName);
  549.         PrintLine(hwndEdit, " (Using Embedded SQL)");
  550.     }
  551.     else
  552.     {
  553.         if (SQLCODE == 100)
  554.         {
  555.             // no rows match the given criteria
  556.             PrintLine(hwndEdit, "That author was not found");
  557.         }
  558.         else
  559.         {
  560.             // problem executing query
  561.             PrintLine(hwndEdit, "ERROR: Executing SELECT query");
  562.             bSuccess = FALSE;
  563.         }
  564.     }
  565.  
  566.     szFirstName[0] = '\0';
  567.     
  568.     // install DB-Library error and message handlers, saving previous
  569.     // handler pointers from Embedded SQL for later re-use
  570.  
  571. #if defined (_WIN32)
  572.     // install handlers with DB-Library
  573.     lpfnOldErrorHandler = dberrhandle((DBERRHANDLE_PROC)DblibErrorHandler);
  574.     lpfnOldMessageHandler = dbmsghandle((DBMSGHANDLE_PROC)DblibMessageHandler);
  575. #else
  576.     // get procedure instances
  577.     lpfnDblibErrorHandler = (DBERRHANDLE_PROC)MakeProcInstance((FARPROC)DblibErrorHandler, hinstMain);
  578.     lpfnDblibMessageHandler = (DBMSGHANDLE_PROC)MakeProcInstance((FARPROC)DblibMessageHandler, hinstMain);
  579.     // install handlers with DB-Library
  580.     lpfnOldErrorHandler = dberrhandle(lpfnDblibErrorHandler);
  581.     lpfnOldMessageHandler = dbmsghandle(lpfnDblibMessageHandler);
  582. #endif
  583.  
  584.     // query using DB-Library
  585.     wsprintf(szSQLCommand,
  586.         "select au_fname from authors where au_lname = '%s'",
  587.         (LPSTR)szLastName);
  588.     dbcmd(pDbproc, szSQLCommand);
  589.  
  590.     // execute the query
  591.     r = dbsqlexec(pDbproc);
  592.     if (r == SUCCEED)
  593.     {
  594.         while (TRUE)
  595.         {
  596.             // get query result set
  597.             r = dbresults(pDbproc);
  598.             switch (r)
  599.             {
  600.             case SUCCEED:
  601.                 while (TRUE)
  602.                 {
  603.                     // get all the rows for this result set
  604.                     s = dbnextrow(pDbproc);
  605.                     switch (s)
  606.                     {
  607.                     case REG_ROW:
  608.                         // put returned column data into string
  609.                         dbconvert(pDbproc,
  610.                             dbcoltype(pDbproc, 1),
  611.                             dbdata(pDbproc, 1),
  612.                             dbdatlen(pDbproc, 1),
  613.                             SQLCHAR,
  614.                             szFirstName,
  615.                             -1);
  616.                         PrintStr(hwndEdit, "That author's first name is: ");
  617.                         PrintStr(hwndEdit, szFirstName);
  618.                         PrintLine(hwndEdit, " (Using DB-Library)");
  619.                         break;
  620.                     case BUF_FULL:
  621.                         break;
  622.                     case NO_MORE_ROWS:
  623.                         break;
  624.                     case FAIL:
  625.                         // dbnextrow() failed
  626.                         PrintLine(hwndEdit, "ERROR: Executing DB-Library query");
  627.                         bSuccess = FALSE;
  628.                         break;
  629.                     default:
  630.                         // compute row
  631.                         break;
  632.                     }
  633.                     if ((s == NO_MORE_ROWS) || (s == FAIL))
  634.                         break; // while loop
  635.                 }
  636.                 break;
  637.  
  638.             case NO_MORE_RESULTS:
  639.                 break;
  640.             case FAIL:
  641.                 // dbresults() failed
  642.                 PrintLine(hwndEdit, "ERROR: Executing DB-Library query");
  643.                 bSuccess = FALSE;
  644.                 break;
  645.  
  646.             }
  647.             if ((r == NO_MORE_RESULTS) || (r == FAIL))
  648.                 break; // while loop
  649.         }
  650.     }
  651.     else
  652.     {
  653.         // dbsqlexec() failed
  654.         PrintLine(hwndEdit, "ERROR: Executing DB-Library query");
  655.         bSuccess = FALSE;
  656.     }
  657.  
  658. #if !defined (_WIN32)
  659.     // free procedure instances
  660.     FreeProcInstance(lpfnDblibErrorHandler);
  661.     FreeProcInstance(lpfnDblibMessageHandler);
  662. #endif
  663.  
  664.     // re-install old Embedded SQL for C error and message handlers
  665.     dberrhandle(lpfnOldErrorHandler);
  666.     dbmsghandle(lpfnOldMessageHandler);
  667.  
  668.     return (bSuccess);
  669.  
  670. }
  671.  
  672. //////////////////////////////////////////////////////////////////////////////
  673. //
  674. //  FUNCTION: ConnectDlgProc()
  675. //
  676. //      Connect box dialog procedure
  677. //
  678. //  MESSAGES:
  679. //
  680. //      WM_INITDIALOG   init dialog
  681. //      WM_COMMAND      button command
  682. //
  683. //  COMMENTS:
  684. //
  685. //////////////////////////////////////////////////////////////////////////////
  686.  
  687. BOOL CALLBACK ConnectDlgProc (
  688.     HWND hwndDlg,
  689.     UINT msg,
  690.     WPARAM wParam,
  691.     LPARAM lParam)
  692. {
  693.     WORD cmdNotify, idItem;
  694.     HWND hwndControl;
  695.  
  696.     switch (msg)
  697.     {
  698.     case WM_INITDIALOG:     // message: initialize dialog box
  699.         SetDlgItemText(hwndDlg,
  700.             IDC_SERVER,
  701.             szServer);
  702.         SetDlgItemText(hwndDlg,
  703.             IDC_LOGIN,
  704.             szLogin);
  705.         SetDlgItemText(hwndDlg,
  706.             IDC_PASSWORD,
  707.             szPassword);
  708.         SetDlgItemText(hwndDlg,
  709.             IDC_DATABASE,
  710.             szDatabase);
  711.         return (TRUE);
  712.         break;
  713.  
  714.     case WM_COMMAND:        // message: received a command
  715.         
  716.         cmdNotify = GET_WM_COMMAND_NOTIFY(wParam, lParam);
  717.         idItem = GET_WM_COMMAND_ID(wParam, lParam);
  718.         hwndControl = GET_WM_COMMAND_HWND(wParam, lParam);
  719.  
  720.         switch (idItem)
  721.         {
  722.         case IDOK:
  723.             // get server name, login, and password
  724.             GetDlgItemText(hwndDlg,
  725.                 IDC_SERVER,
  726.                 szServer,
  727.                 SQLID_MAX);
  728.             GetDlgItemText(hwndDlg,
  729.                 IDC_LOGIN,
  730.                 szLogin,
  731.                 SQLID_MAX);
  732.             GetDlgItemText(hwndDlg,
  733.                 IDC_PASSWORD,
  734.                 szPassword,
  735.                 SQLID_MAX);
  736.             GetDlgItemText(hwndDlg,
  737.                 IDC_DATABASE,
  738.                 szDatabase,
  739.                 SQLID_MAX);
  740.  
  741.             PostMessage(hwndMain, WM_CONNECTTOSQLSERVER, 0, 0);            
  742.  
  743.             EndDialog(hwndDlg, TRUE);
  744.             return (TRUE);
  745.             break;
  746.         case IDCANCEL:
  747.             EndDialog(hwndDlg, TRUE);
  748.             return (TRUE);
  749.             break;
  750.         }
  751.         break;
  752.     }
  753.     return (FALSE);               // didn't process a message
  754. }
  755.  
  756. //////////////////////////////////////////////////////////////////////////////
  757. //
  758. //  FUNCTION: QueryDlgProc()
  759. //
  760. //      Query box dialog procedure
  761. //
  762. //  MESSAGES:
  763. //
  764. //      WM_INITDIALOG   init dialog
  765. //      WM_COMMAND      button command
  766. //
  767. //  COMMENTS:
  768. //
  769. //////////////////////////////////////////////////////////////////////////////
  770.  
  771. BOOL CALLBACK QueryDlgProc (
  772.     HWND hwndDlg,
  773.     UINT msg,
  774.     WPARAM wParam,
  775.     LPARAM lParam)
  776. {
  777.     WORD cmdNotify, idItem;
  778.     HWND hwndControl;
  779.  
  780.     switch (msg)
  781.     {
  782.     case WM_INITDIALOG:     // message: initialize dialog box
  783.         return (TRUE);
  784.         break;
  785.  
  786.     case WM_COMMAND:        // message: received a command
  787.         
  788.         cmdNotify = GET_WM_COMMAND_NOTIFY(wParam, lParam);
  789.         idItem = GET_WM_COMMAND_ID(wParam, lParam);
  790.         hwndControl = GET_WM_COMMAND_HWND(wParam, lParam);
  791.  
  792.         switch (idItem)
  793.         {
  794.         case IDOK:
  795.             // get author's last name for query
  796.             GetDlgItemText(hwndDlg,
  797.                 IDC_LASTNAME,
  798.                 szLastName,
  799.                 49);
  800.  
  801.             PostMessage(hwndMain, WM_QUERYSQLSERVER, 0, 0);            
  802.  
  803.             EndDialog(hwndDlg, TRUE);
  804.             return (TRUE);
  805.             break;
  806.         case IDCANCEL:
  807.             EndDialog(hwndDlg, TRUE);
  808.             return (TRUE);
  809.             break;
  810.         }
  811.         break;
  812.     }
  813.     return (FALSE);               // didn't process a message
  814. }
  815.  
  816. //////////////////////////////////////////////////////////////////////////////
  817. //
  818. //  FUNCTION: AboutDlgProc()
  819. //
  820. //      About box dialog procedure
  821. //
  822. //  MESSAGES:
  823. //
  824. //      WM_INITDIALOG   init dialog
  825. //      WM_COMMAND      button command
  826. //
  827. //  COMMENTS:
  828. //
  829. //////////////////////////////////////////////////////////////////////////////
  830.  
  831. BOOL CALLBACK AboutDlgProc (
  832.     HWND hwndDlg,
  833.     UINT msg,
  834.     WPARAM wParam,
  835.     LPARAM lParam)
  836. {
  837.     WORD cmdNotify, idItem;
  838.     HWND hwndControl;
  839.  
  840.     switch (msg)
  841.     {
  842.     case WM_INITDIALOG:     // message: initialize dialog box
  843.         SetDlgItemText(hwndDlg,
  844.             IDC_PLATFORM,
  845.             OS_STRING);
  846.         return (TRUE);
  847.         break;
  848.  
  849.     case WM_COMMAND:        // message: received a command
  850.         
  851.         cmdNotify = GET_WM_COMMAND_NOTIFY(wParam, lParam);
  852.         idItem = GET_WM_COMMAND_ID(wParam, lParam);
  853.         hwndControl = GET_WM_COMMAND_HWND(wParam, lParam);
  854.  
  855.         switch (idItem)
  856.         {
  857.         case IDOK:
  858.         case IDCANCEL:
  859.             EndDialog(hwndDlg, TRUE);
  860.             return (TRUE);
  861.             break;
  862.         }
  863.         break;
  864.     }
  865.     return (FALSE);               // didn't process a message
  866. }
  867.  
  868. ///////////////////////////////////////////////////////////////////////////////
  869. //
  870. //  FUNCTION: ErrorHandler()
  871. //
  872. //      Called on Embedded SQL for C error, displays fields from SQLCA
  873. //
  874. //  PARAMETERS: none
  875. //
  876. //  RETURNS: none
  877. //
  878. //  COMMENTS:
  879. //
  880. ///////////////////////////////////////////////////////////////////////////////
  881.  
  882. void ErrorHandler (void)
  883. {
  884.     char szScratch[200] = "";
  885.  
  886.     PrintLine(hwndEdit, "Embedded SQL Error Handler called:");
  887.     wsprintf(szScratch, "    SQL Code %li", SQLCODE);
  888.     PrintLine(hwndEdit, szScratch);
  889.     wsprintf(szScratch, "    SQL Server Message %li: '%s'", SQLERRD1, (LPSTR)SQLERRMC);
  890.     PrintLine(hwndEdit, szScratch);
  891. }
  892.  
  893. ///////////////////////////////////////////////////////////////////////////////
  894. //
  895. //  FUNCTION: DblibErrorHandler()
  896. //
  897. //      Called when a DB-Library error occurs
  898. //
  899. //  PARAMETERS:
  900. //
  901. //      pDbproc - pointer to DBPROCESS connection
  902. //      nDblibSeverity - DB-Library error severity
  903. //      nDblibErrorNo - DB-Library error number
  904. //      nOSErrorNo - operating system error number
  905. //      lpstrDblibError - DB-Library error string
  906. //      lpstrOSError - operating system error string
  907. //
  908. //  RETURNS: One of three DB-Library return continuation codes:
  909. //
  910. //      INT_CANCEL
  911. //      INT_CONTINUE
  912. //      INT_EXIT
  913. //
  914. //  COMMENTS:
  915. //
  916. ///////////////////////////////////////////////////////////////////////////////
  917.  
  918. int FAR EXPORT DblibErrorHandler (
  919.     PDBPROCESS pDbproc,
  920.     int nDblibSeverity,
  921.     int nDblibErrorNo,
  922.     int nOSErrorNo,
  923.     LPCSTR lpstrDblibError,
  924.     LPCSTR lpstrOSError)
  925. {
  926.     char szScratch[200] = "";
  927.  
  928.     PrintLine(hwndEdit, "DB-Library Error Handler called:");
  929.     wsprintf(szScratch, "    DB-Library Error %i: %s",
  930.         nDblibErrorNo, (LPSTR)lpstrDblibError);
  931.     PrintLine(hwndEdit, szScratch);
  932.  
  933.     if (nOSErrorNo != DBNOERR)
  934.     {
  935.         // operating system error
  936.         wsprintf(szScratch, "    Operating System Error %i: %s",
  937.             nOSErrorNo, (LPSTR)lpstrOSError);
  938.         PrintLine(hwndEdit, szScratch);
  939.     }
  940.  
  941.     // this will cause the offending DB-Library function to return FAIL
  942.     return(INT_CANCEL);
  943. }
  944.  
  945. ///////////////////////////////////////////////////////////////////////////////
  946. //
  947. //  FUNCTION: DblibMessageHandler()
  948. //
  949. //      Called when a SQL Server message is received
  950. //
  951. //  PARAMETERS:
  952. //
  953. //      pDbproc - pointer to DBPROCESS connection
  954. //      lSQLMessageNo - SQL Server message number
  955. //      nSQLState - SQL Server message state
  956. //      nSQLSeverity - SQL Server message severity
  957. //      lpstrSQLMessage - SQL Server message string
  958. //
  959. //  RETURNS: 0
  960. //
  961. //  COMMENTS:
  962. //
  963. ///////////////////////////////////////////////////////////////////////////////
  964.  
  965. int FAR EXPORT DblibMessageHandler (
  966.     PDBPROCESS pDbproc,
  967.     DBINT lSQLMessageNo,
  968.     int nSQLState,
  969.     int nSQLSeverity,
  970.     LPCSTR lpstrSQLMessage)
  971. {
  972.     char szScratch[200] = "";
  973.  
  974.     PrintLine(hwndEdit, "DB-Library Message Handler called:");
  975.     wsprintf(szScratch, "    SQL Server Message %li: %s",
  976.         lSQLMessageNo, (LPSTR)lpstrSQLMessage);
  977.     PrintLine(hwndEdit, szScratch);
  978.  
  979.     return(0);
  980. }
  981.