home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / ptk / i386 / sqlcursw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-12  |  28.3 KB  |  870 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: SqlCursw.c
  4.          Copyright (C) 1995 Microsoft Corp.
  5.  
  6.     PURPOSE: Sql Server sample Windows applications
  7.  
  8.     COMMENTS:
  9.  
  10.     Windows can have several copies of your application running at the
  11.     same time.  The variable hInst keeps track of which instance this
  12.     application is so that processing will be to the correct window.
  13.  
  14.     You only need to initialize the application once.  After it is
  15.     initialized, all other copies of the application will use the same
  16.     window class, and do not need to be separately initialized.
  17.  
  18. ****************************************************************************/
  19.  
  20. #include "windows.h"            /* required for all Windows applications*/
  21. #define DBMSWIN                /* needed to define environment         */
  22. #include "sqlfront.h"            /* standard dblib include file        */
  23. #include "sqldb.h"            /* standard dblib include file        */
  24. #include "sqlcursw.h"            /* specific to this program         */
  25.  
  26. PDBPROCESS dbproc = (PDBPROCESS)NULL;
  27. PDBCURSOR  hdbcursor = (PDBCURSOR)NULL;
  28.  
  29. char stmt[] = "select au_lname, au_fname, city, state from authors where contract = 1 ";
  30.  
  31. /* Status array, and results set */
  32.  
  33. DBINT        pstat[NROWS];
  34. DBINT        plen[NROWS];
  35. char        au_lname[NROWS][41];
  36. char        au_fname[NROWS][21];
  37. char        au_city[NROWS][21];
  38. char        au_state[NROWS][3];
  39. char        szRowNum[5];
  40. USHORT        rownum = 0;
  41. char        szValues[251];
  42. char        szTable[31];
  43.  
  44. HANDLE hInst;                /* current instance                */
  45. HWND ghWnd;                /* global window handle for handlers    */
  46. HWND errhWnd;                /* global window handle for current error*/
  47. /****************************************************************************
  48.  
  49.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  50.  
  51.     PURPOSE: calls initialization function, processes message loop
  52.  
  53.     COMMENTS:
  54.  
  55.     This will initialize the window class if it is the first time this
  56.     application is run.  It then creates the window, and processes the
  57.     message loop until a PostQuitMessage is received.  It exits the
  58.     application by returning the value passed by the PostQuitMessage.
  59.  
  60. ****************************************************************************/
  61.  
  62. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  63. {
  64.     HWND hWnd;                     /* window handle             */
  65.     MSG msg;                     /* message                 */
  66.  
  67.  
  68.     if (!hPrevInstance)            /* Has application been initialized? */
  69.     if (!SqlTestInit(hInstance))
  70.         return (NULL);        /* Exits if unable to initialize     */
  71.  
  72.     hInst = hInstance;            /* Saves the current instance         */
  73.  
  74.     hWnd = CreateWindow("SQL Test",          /* window class         */
  75.     "SQL Server Sample Windows Application",  /* window name         */
  76.     WS_OVERLAPPEDWINDOW,              /* window style         */
  77.     CW_USEDEFAULT,                  /* x position             */
  78.     CW_USEDEFAULT,                  /* y position             */
  79.     CW_USEDEFAULT,                  /* width             */
  80.     CW_USEDEFAULT,                  /* height             */
  81.     NULL,                      /* parent handle         */
  82.     NULL,                      /* menu or child ID         */
  83.     hInstance,                  /* instance             */
  84.     NULL);                      /* additional info         */
  85.  
  86.     if (!hWnd)                      /* Was the window created? */
  87.     return (NULL);
  88.  
  89.     ghWnd = hWnd;                  /* set global handle         */
  90.     errhWnd = hWnd;
  91.  
  92.     ShowWindow(hWnd, nCmdShow);              /* Shows the window         */
  93.     UpdateWindow(hWnd);                  /* Sends WM_PAINT message  */
  94.  
  95.     while (GetMessage(&msg,       /* message structure                 */
  96.         NULL,           /* handle of window receiving the message */
  97.         NULL,           /* lowest message to examine             */
  98.         NULL))           /* highest message to examine         */
  99.     {
  100.     TranslateMessage(&msg);       /* Translates virtual key codes         */
  101.     DispatchMessage(&msg);       /* Dispatches message to window         */
  102.     }
  103.     return (msg.wParam);       /* Returns the value from PostQuitMessage */
  104. }
  105.  
  106.  
  107. /****************************************************************************
  108.  
  109.     FUNCTION: SqlTestInit(HANDLE)
  110.  
  111.     PURPOSE: Initializes window data and registers window class
  112.  
  113.     COMMENTS:
  114.  
  115.     Sets up a structure to register the window class.  Structure includes
  116.     such information as what function will process messages, what cursor
  117.     and icon to use, etc.
  118.  
  119.  
  120. ****************************************************************************/
  121.  
  122. BOOL SqlTestInit(HANDLE hInstance)
  123. {
  124.     HANDLE hMemory;                   /* handle to allocated memory */
  125.     PWNDCLASS pWndClass;               /* structure pointer         */
  126.     BOOL bSuccess;                   /* RegisterClass() result     */
  127.  
  128.     hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  129.     pWndClass = (PWNDCLASS) LocalLock(hMemory);
  130.  
  131.     pWndClass->style = NULL; /*CS_HREDRAW | CS_VREDRAW; */
  132.     pWndClass->lpfnWndProc = SqlTestWndProc;
  133.     pWndClass->hInstance = hInstance;
  134.     pWndClass->hIcon = LoadIcon(hInstance, "SQLITEST");
  135.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  136.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  137.     pWndClass->lpszMenuName = (LPSTR)"SQLTest";
  138.     pWndClass->lpszClassName = (LPSTR)"SQL Test";
  139.  
  140.     bSuccess = RegisterClass(pWndClass);
  141.  
  142.  
  143.     LocalUnlock(hMemory);                /* Unlocks the memory    */
  144.     LocalFree(hMemory);                    /* Returns it to Windows */
  145.     return (bSuccess);         /* Returns result of registering the window */
  146. }
  147.  
  148. /****************************************************************************
  149.  
  150.     FUNCTION: SqlTestWndProc(HWND, unsigned, WPARAM, LPARAM)
  151.  
  152.     PURPOSE:  Processes messages
  153.  
  154.     MESSAGES:
  155.  
  156.     WM_SYSCOMMAND - system menu (About dialog box)
  157.     WM_CREATE     - create window
  158.     WM_DESTROY    - destroy window
  159.     WM_COMMAND    - application menus (Connect and Select dialog boxes
  160.  
  161.     COMMENTS:
  162.  
  163.     To process the ID_ABOUTSQL message, call MakeProcInstance() to get the
  164.     current instance address of the About() function.  Then call Dialog
  165.     box which will create the box according to the information in your
  166.     SqlTest.rc file and turn control over to the About() function.    When
  167.     it returns, free the intance address.
  168.     This same action will take place for the two menu items Connect and
  169.     Select.
  170.  
  171.  
  172. ****************************************************************************/
  173.  
  174. long FAR PASCAL SqlTestWndProc(HWND hWnd, WORD message, WPARAM wParam, LPARAM lParam)
  175. {
  176.     FARPROC lpProcAbout;          /* pointer to the "About" function */
  177.     FARPROC lpProcRowNum;          /* pointer to the "GetRowNum" function */
  178.     FARPROC lpProcModify;          /* pointer to the "ModifyRow" function */
  179.     FARPROC lpProcSQL;              /* pointer to the "ConnectSQL" function*/
  180.  
  181.     HMENU hMenu;              /* handle to the System menu         */
  182.     static FARPROC lpdbwinMessageHandler; /* pointer to message handler         */
  183.     static FARPROC lpdbwinErrorHandler;   /* pointer to error handler         */
  184.     RETCODE    rc;              /* return code             */
  185.  
  186.     switch (message) {
  187.     case WM_SYSCOMMAND:        /* message: command from system menu */
  188.         if (wParam == ID_ABOUTSQL) {
  189.         lpProcAbout = MakeProcInstance(AboutSQL, hInst);
  190.  
  191.         DialogBox(hInst,         /* current instance         */
  192.             "ABOUTSQL",             /* resource to use         */
  193.             hWnd,             /* parent handle         */
  194.             lpProcAbout);         /* About() instance address */
  195.  
  196.         FreeProcInstance(lpProcAbout);
  197.         break;
  198.         }
  199.  
  200.         else                /* Lets Windows process it         */
  201.         return (DefWindowProc(hWnd, message, wParam, lParam));
  202.  
  203.     case WM_CREATE:                /* message: window being created */
  204.  
  205.         /* Get the handle of the System menu */
  206.  
  207.         hMenu = GetSystemMenu(hWnd, FALSE);
  208.  
  209.         /* Add a separator to the menu */
  210.  
  211.         ChangeMenu(hMenu,                  /* menu handle         */
  212.         NULL,                      /* menu item to change */
  213.         NULL,                      /* new menu item         */
  214.         NULL,                      /* menu identifier     */
  215.         MF_APPEND | MF_SEPARATOR);          /* type of change         */
  216.  
  217.         /* Add new menu item to the System menu */
  218.  
  219.         ChangeMenu(hMenu,                  /* menu handle         */
  220.         NULL,                      /* menu item to change */
  221.         "A&bout SQL Test...",              /* new menu item         */
  222.         ID_ABOUTSQL,                  /* menu identifier     */
  223.         MF_APPEND | MF_STRING);              /* type of change         */
  224.     
  225.                         /* Now make the message and error    */
  226.                     /* handler instances             */
  227.             dbinit();
  228.         lpdbwinMessageHandler =
  229.             MakeProcInstance((FARPROC)dbwinMessageHandler, hInst);
  230.         lpdbwinErrorHandler =
  231.             MakeProcInstance((FARPROC)dbwinErrorHandler, hInst);
  232.                     /* Install the instances into dblib */    
  233.         dbmsghandle(lpdbwinMessageHandler);
  234.         dberrhandle(lpdbwinErrorHandler);
  235.         MessageBox(hWnd,
  236.           (LPSTR)"None of the data modifications will be committed",
  237.           (LPSTR)"SQLCURSW", MB_OK);
  238.         break;
  239.     
  240.     case WM_COMMAND :            /* menu selections generate */
  241.                         /* the WM_COMMAND message   */    
  242.         switch(wParam)            /* menu in WORD parameter   */
  243.         {
  244.         case IDM_CONNECT :        /* connect to server        */
  245.             lpProcSQL = MakeProcInstance(ConnectSQL, hInst);
  246.  
  247.             DialogBox(hInst,        /* current instance         */
  248.             "CONNECT",         /* resource to use         */
  249.             hWnd,            /* parent handle         */
  250.             lpProcSQL);         /* ConnectSQL() instance address */
  251.  
  252.             FreeProcInstance(lpProcSQL);
  253.             break;
  254.         case IDM_RAND :
  255.         case IDM_RELT :
  256.         case IDM_REFRESH :
  257.             /* Obtain the row number */
  258.             lpProcRowNum = MakeProcInstance(GetRowNum, hInst);
  259.             DialogBox(hInst, "ROWNUM", hWnd, lpProcRowNum);
  260.             FreeProcInstance(lpProcRowNum);
  261.  
  262.             /* FALL THROUGH */
  263.  
  264.         case IDM_FIRST :
  265.         case IDM_NEXT :
  266.         case IDM_PREV :
  267.         case IDM_LAST :
  268.             DBLOCKLIB();
  269.             if (wParam == IDM_REFRESH)
  270.             rc = dbcursor(hdbcursor, wParam - UPD_CONST, rownum,
  271.                 NULL, NULL);
  272.             else
  273.             /* Do the fetch and display of rows */
  274.             rc = dbcursorfetchex(hdbcursor, wParam - FETCH_CONST, rownum, NROWS, 0);
  275.             if (rc == SUCCEED)
  276.             SQLTestProcessResults(hWnd);
  277.             DBUNLOCKLIB();
  278.             rownum = 0;     /* Reset for next fetch */
  279.             break;
  280.         case IDM_DELETE :
  281.         case IDM_LOCK :
  282.         case IDM_UPDATE :
  283.         case IDM_INSERT :
  284.             szValues[0] = '\0';
  285.             szTable[0] = '\0';
  286.             /* Obtain the row number */
  287.             if (wParam == IDM_DELETE || wParam == IDM_LOCK)
  288.             {
  289.             lpProcModify = MakeProcInstance(DelLockRow, hInst);
  290.             DialogBox(hInst, "DELLOCK", hWnd, lpProcModify);
  291.             }
  292.             else
  293.             {
  294.             lpProcModify = MakeProcInstance(ModifyRow, hInst);
  295.             DialogBox(hInst, "MODIFY", hWnd, lpProcModify);
  296.             }
  297.             FreeProcInstance(lpProcModify);
  298.             DBLOCKLIB();
  299.             /* Do the dbcursor call */
  300.             dbcursor(hdbcursor, wParam - UPD_CONST, rownum,
  301.               szTable, szValues);
  302.             DBUNLOCKLIB();
  303.             rownum = 0;     /* Reset for next command */
  304.             break;
  305.  
  306.         }
  307.         break;
  308.     
  309.     case WM_DESTROY:          /* message: window being destroyed */
  310.         dbexit();              /* free any active dbprocesses     */
  311.         FreeProcInstance(lpdbwinMessageHandler);    /* release handlers  */
  312.         FreeProcInstance(lpdbwinErrorHandler);
  313.             dbwinexit();
  314.         PostQuitMessage(0);
  315.         break;
  316.  
  317.     default:              /* Passes it on if unproccessed    */
  318.         return (DefWindowProc(hWnd, message, wParam, lParam));
  319.     }
  320.     return (NULL);
  321. }
  322.  
  323.  
  324. /****************************************************************************
  325.  
  326.     FUNCTION: AboutSQL(HWND, WORD, WPARAM, LPARAM)
  327.  
  328.     PURPOSE:  Processes messages for "AboutSQL" dialog box
  329.  
  330.     MESSAGES:
  331.  
  332.     WM_INITDIALOG - initialize dialog box
  333.     WM_COMMAND    - Input received
  334.  
  335.     COMMENTS:
  336.  
  337.     No initialization is needed for this particular dialog box, but TRUE
  338.     must be returned to Windows.
  339.  
  340.     Wait for user to click on "Ok" button, then close the dialog box.
  341.  
  342. ****************************************************************************/
  343.  
  344. BOOL FAR PASCAL AboutSQL(HWND hDlg, WORD message, WPARAM wParam, LPARAM lParam)
  345. {
  346.     switch (message) {
  347.     case WM_INITDIALOG:           /* message: initialize dialog box */
  348.         return (TRUE);
  349.  
  350.     case WM_COMMAND:              /* message: received a command */
  351.         if (wParam == IDOK) {          /* "OK" box selected?         */
  352.         EndDialog(hDlg, NULL);          /* Exits the dialog box         */
  353.         return (TRUE);
  354.         }
  355.         break;
  356.     }
  357.     return (FALSE);                  /* Didn't process a message    */
  358. }
  359.  
  360.  
  361. /****************************************************************************
  362.  
  363.     FUNCTION: GetRowNum(HWND, WORD, WPARAM, LPARAM)
  364.  
  365.     PURPOSE:  Processes messages for "ROWNUM" dialog box
  366.  
  367.     MESSAGES:
  368.  
  369.     WM_INITDIALOG - initialize dialog box
  370.     WM_COMMAND    - Input received
  371.  
  372.     COMMENTS:
  373.  
  374.     No initialization is needed for this particular dialog box, but TRUE
  375.     must be returned to Windows.
  376.  
  377.     Wait for user to click on "Ok" button, then close the dialog box.
  378.  
  379. ****************************************************************************/
  380.  
  381. BOOL FAR PASCAL GetRowNum(HWND hDlg, WORD message, WPARAM wParam, LPARAM lParam)
  382. {
  383.     switch (message) {
  384.     case WM_INITDIALOG:           /* message: initialize dialog box */
  385.         SendDlgItemMessage(hDlg,       /* limit input to 4 characters  */
  386.         ID_ROWNUM,EM_LIMITTEXT,4,0L);
  387.         return (TRUE);
  388.  
  389.     case WM_COMMAND:              /* message: received a command */
  390.         if (wParam == IDOK) {          /* "OK" box selected?         */
  391.         GetDlgItemText(hDlg,ID_ROWNUM,
  392.             (LPSTR)szRowNum,4);       /* Get row number */
  393.         szRowNum[4] = '\0';
  394.         rownum = atoi(szRowNum);
  395.         EndDialog(hDlg, NULL);          /* Exits the dialog box         */
  396.         return (TRUE);
  397.         }
  398.         break;
  399.     }
  400.     return (FALSE);                  /* Didn't process a message    */
  401. }
  402.  
  403.  
  404. /****************************************************************************
  405.  
  406.     FUNCTION: DelLockRow(HWND, unsigned, WPARAM, LPARAM)
  407.  
  408.     PURPOSE:  Processes messages for "DELLOCK" dialog box
  409.  
  410.     MESSAGES:
  411.  
  412.     WM_INITDIALOG - initialize dialog box
  413.     WM_COMMAND    - Input received
  414.  
  415.     COMMENTS:
  416.  
  417.     No initialization is needed for this particular dialog box, but TRUE
  418.     must be returned to Windows.
  419.  
  420.     Wait for user to click on "Ok" button, then close the dialog box.
  421.  
  422. ****************************************************************************/
  423.  
  424. BOOL FAR PASCAL DelLockRow(HWND hDlg, WORD message, WPARAM wParam, LPARAM lParam)
  425. {
  426.     switch (message) {
  427.     case WM_INITDIALOG:           /* message: initialize dialog box */
  428.         SendDlgItemMessage(hDlg,       /* limit row# input to 4 characters*/
  429.         ID_DROWNUM,EM_LIMITTEXT,4,0L);
  430.         SendDlgItemMessage(hDlg,       /* limit table input to 30 characters*/
  431.         ID_DTABLE,EM_LIMITTEXT,30,0L);
  432.         return (TRUE);
  433.  
  434.     case WM_COMMAND:              /* message: received a command */
  435.         if (wParam == IDOK) {          /* "OK" box selected?         */
  436.         GetDlgItemText(hDlg,ID_DROWNUM,
  437.             (LPSTR)szRowNum,4);       /* Get row number */
  438.         szRowNum[4] = '\0';
  439.         rownum = atoi(szRowNum);
  440.         GetDlgItemText(hDlg,ID_DTABLE,
  441.             (LPSTR)szTable,30);       /* Get table name */
  442.         EndDialog(hDlg, NULL);          /* Exits the dialog box         */
  443.         return (TRUE);
  444.         }
  445.         else if (wParam == IDCANCEL)
  446.         {
  447.         EndDialog(hDlg, NULL);          /* Exits the dialog box         */
  448.         return (TRUE);
  449.         }
  450.         break;
  451.     }
  452.     return (FALSE);                  /* Didn't process a message    */
  453. }
  454.  
  455.  
  456. /****************************************************************************
  457.  
  458.     FUNCTION: ModifyRow(HWND, WORD, WPARAM, LPARAM)
  459.  
  460.     PURPOSE:  Processes messages for "MODIFY" dialog box
  461.  
  462.     MESSAGES:
  463.  
  464.     WM_INITDIALOG - initialize dialog box
  465.     WM_COMMAND    - Input received
  466.  
  467.     COMMENTS:
  468.  
  469.     No initialization is needed for this particular dialog box, but TRUE
  470.     must be returned to Windows.
  471.  
  472.     Wait for user to click on "Ok" button, then close the dialog box.
  473.  
  474. ****************************************************************************/
  475.  
  476. BOOL FAR PASCAL ModifyRow(HWND hDlg, WORD message, WPARAM wParam, LPARAM lParam)
  477. {
  478.     switch (message) {
  479.     case WM_INITDIALOG:           /* message: initialize dialog box */
  480.         SendDlgItemMessage(hDlg,       /* limit row# input to 4 characters*/
  481.         ID_MROWNUM,EM_LIMITTEXT,4,0L);
  482.         SendDlgItemMessage(hDlg,       /* limit table input to 30 characters*/
  483.         ID_MTABLE,EM_LIMITTEXT,30,0L);
  484.         SendDlgItemMessage(hDlg,       /* limit values input to 250 characters*/
  485.         ID_MVALUES,EM_LIMITTEXT,250,0L);
  486.         return (TRUE);
  487.  
  488.     case WM_COMMAND:              /* message: received a command */
  489.         if (wParam == IDOK) {          /* "OK" box selected?         */
  490.         GetDlgItemText(hDlg,ID_MROWNUM,
  491.             (LPSTR)szRowNum,4);       /* Get row number */
  492.         szRowNum[4] = '\0';
  493.         rownum = atoi(szRowNum);
  494.         GetDlgItemText(hDlg,ID_MTABLE,
  495.             (LPSTR)szTable,30);       /* Get table name */
  496.         GetDlgItemText(hDlg,ID_MVALUES,
  497.             (LPSTR)szValues,250);     /* Get values string */
  498.         EndDialog(hDlg, NULL);          /* Exits the dialog box         */
  499.         return (TRUE);
  500.         }
  501.         else if (wParam == IDCANCEL)
  502.         {
  503.         EndDialog(hDlg, NULL);          /* Exits the dialog box         */
  504.         return (TRUE);
  505.         }
  506.         break;
  507.     }
  508.     return (FALSE);                  /* Didn't process a message    */
  509. }
  510.  
  511.  
  512. /****************************************************************************
  513.  
  514.     FUNCTION: ConnectSQL(HWND, WORD, WPARAM, LPARAM)
  515.  
  516.     PURPOSE:  Processes messages for "Connect" dialog box
  517.  
  518.     MESSAGES:
  519.  
  520.     WM_INITDIALOG - initialize dialog box
  521.     WM_COMMAND    - Input received
  522.  
  523.     COMMENTS:
  524.  
  525.     No initialization is needed for this particular dialog box, but TRUE
  526.     must be returned to Windows.
  527.  
  528.     Wait for user to click on "Ok" button, then close the dialog box.
  529.  
  530. ****************************************************************************/
  531.  
  532. BOOL FAR PASCAL ConnectSQL(HWND hDlg, WORD message, WPARAM wParam, LPARAM lParam)
  533. {
  534.     char szSQLServer[31];
  535.     char szServerMess[81];
  536.     static PLOGINREC LoginRec;
  537.     RETCODE    rc;
  538.  
  539.     *szSQLServer = NULL;
  540.     switch (message) {
  541.     case WM_INITDIALOG:           /* message: initialize dialog box*/
  542.         SendDlgItemMessage(hDlg,       /* limit input to 30 characters  */
  543.         ID_SQLSERVER,EM_LIMITTEXT,30,0L);
  544.         return (TRUE);
  545.  
  546.     case WM_COMMAND:              /* message: received a command*/
  547.         errhWnd = hDlg;
  548.         switch(wParam)
  549.         {
  550.         case IDOK :              /* "OK" box selected?        */
  551.             GetDlgItemText(hDlg,ID_SQLSERVER,
  552.             (LPSTR)szSQLServer,
  553.                 MAX_SERVERNAME); /* get Server name */
  554.             if(*szSQLServer != NULL) /* was something input        */
  555.             {
  556.             DBLOCKLIB();        /* lock down library        */
  557.             if(dbproc != (PDBPROCESS)NULL) /* if an active     */
  558.                                 /* process close it */
  559.             {
  560.                 dbclose(dbproc);
  561.                 dbproc = (PDBPROCESS)NULL;
  562.             }
  563.             if((LoginRec = dblogin()) != (PLOGINREC)NULL) /* get loginrec */
  564.             {
  565.                 DBSETLUSER(LoginRec,(char far *)"sa"); /* set user  */
  566.                  DBSETLVERSION(LoginRec,DBVER60); /* Request 6.0 server behavior */
  567.  
  568.                     /* now open the connection to server */
  569.                 if((dbproc = dbopen(LoginRec,(LPSTR)szSQLServer))
  570.                      == (PDBPROCESS)NULL)
  571.                 {
  572.                     /* if NULL couldn't connect    */
  573.                 dbfreelogin(LoginRec);
  574.                 }
  575.                 else /* got connect so use the pubs database */
  576.                 {
  577.                 dbuse(dbproc,(LPSTR)"pubs");
  578.                 dbfreelogin(LoginRec);
  579.                 LoginRec = (PLOGINREC)NULL;
  580.                 }
  581.             }
  582.             else /* memory allocation problem */
  583.                 MessageBox(hDlg, "Could not allocate Login Record",
  584.                 "System Error", MB_ICONHAND | MB_OK);
  585.  
  586.             /* Open the cursor, bind variables */
  587.             if (SQLInitCursor() == FALSE)
  588.                 MessageBox(hDlg, "Cursor open failed",
  589.                 "System Error", MB_ICONHAND | MB_OK);
  590.             DBUNLOCKLIB(); /* done unlock library    */
  591.             }
  592.             EndDialog(hDlg, NULL);          /* Exits the dialog box         */
  593.             return (TRUE);
  594.             break;
  595.         case IDCANCEL :
  596.             EndDialog(hDlg, NULL);
  597.             return(TRUE);
  598.             break;
  599.         
  600.         }
  601.         break;
  602.     }
  603.     return (FALSE);                  /* Didn't process a message    */
  604. }
  605.  
  606.  
  607. /****************************************************************************
  608.  
  609.     FUNCTION: SQLInitCursor()
  610.  
  611.     PURPOSE:  Initialize cursor, bind variables
  612.  
  613.     PARAMETERS: NONE
  614.  
  615.     RETURNS: NONE
  616.  
  617.     COMMENTS:
  618.  
  619. ****************************************************************************/
  620. BOOL PASCAL SQLInitCursor()
  621. {
  622.     int     i, j= 0;
  623.     RETCODE  rc;
  624.  
  625.     /* Open the cursor */
  626.     hdbcursor = dbcursoropen(dbproc, stmt,
  627.     KEYSET, CONCUROPT, NROWS, pstat);
  628.     if (hdbcursor == (PDBCURSOR)NULL)
  629.     {
  630.     return FALSE;
  631.     }
  632.  
  633.     /* Start a transaction block. We will exit without
  634.     ** committing, so the pubs database will
  635.     ** not be altered.
  636.     */
  637.     if ((dbcmd(dbproc, "begin tran") != SUCCEED) ||
  638.       (dbsqlexec(dbproc) != SUCCEED))
  639.     {
  640.     /* Error! Close the connection and exit */
  641.     dbclose(dbproc);
  642.     dbproc = (PDBPROCESS)NULL;
  643.     return FALSE;
  644.     }
  645.     while ((rc=dbresults(dbproc)) != NO_MORE_RESULTS)
  646.     {
  647.     if (rc == FAIL)
  648.     {
  649.       /* Error! Close the connection and exit */
  650.       dbclose(dbproc);
  651.       dbproc = (PDBPROCESS)NULL;
  652.       return FALSE;
  653.     }
  654.     }
  655.     /* Bind variables */
  656.     rc = dbcursorbind(hdbcursor, 1, NTBSTRINGBIND, 41, NULL,
  657.     (char far *)au_lname);
  658.     if (rc == FAIL)
  659.     {
  660.     return FALSE;
  661.     }
  662.     rc = dbcursorbind(hdbcursor, 2, NTBSTRINGBIND, 21, NULL,
  663.     (char far *)au_fname);
  664.     if (rc == FAIL)
  665.     {
  666.     return FALSE;
  667.     }
  668.     rc = dbcursorbind(hdbcursor, 3, NTBSTRINGBIND, 21, NULL,
  669.     (char far *)au_city);
  670.     if (rc == FAIL)
  671.     {
  672.     return FALSE;
  673.     }
  674.     rc = dbcursorbind(hdbcursor, 4, NTBSTRINGBIND, 3, NULL,
  675.     (char far *)au_state);
  676.     if (rc == FAIL)
  677.     {
  678.     return FALSE;
  679.     }
  680.     return (TRUE);
  681. }
  682.  
  683.  
  684. /****************************************************************************
  685.  
  686.     FUNCTION: CheckForScroll(HWND, int, int, int)
  687.  
  688.     PURPOSE:  Check if next output line will be out of client area
  689.  
  690.     PARAMETERS: hWnd - Handle to the window.
  691.         CurrentPosition - Current y coordinate for the line of
  692.             text just written to the client area.
  693.         Spacing - The height of the line (including the space
  694.             separating lines) of the text just written.
  695.         Length - The length of the line just written in device
  696.             units.
  697.  
  698.     RETURN:    Returns the Y coordinate for the next line of text.
  699.  
  700.     COMMENTS:
  701.  
  702.     Will determine if the next line of text will be out of the client
  703.     area.  If so will scroll the window for the next line.  Also validates
  704.     the current line of text so that a WM_PAINT will not clear it.
  705.  
  706. ****************************************************************************/
  707. int CheckForScroll(HWND hWnd, int CurrentPosition, int Spacing, int Length)
  708. {
  709.     RECT rect;                /* RECT structure for validation */
  710.     rect.top = CurrentPosition;     /* top of last line of text     */
  711.     rect.bottom = CurrentPosition+Spacing+1; /* bottom of last line     */
  712.     rect.left = 1;            /* left most column of line     */
  713.     rect.right = Length+1;        /* right most column of line     */
  714.     ValidateRect(hWnd,(LPRECT)&rect);   /* validate line so that it is   */
  715.                     /* not blanked on next paint     */
  716.         
  717.     GetClientRect(hWnd,(LPRECT)&rect);    /* get rect for current client   */
  718.     if(CurrentPosition + (Spacing*2) > rect.bottom) /* will line fit     */
  719.     {
  720.                     /* if not scroll window and      */
  721.                     /* update client window         */
  722.     ScrollWindow(hWnd,0,-(Spacing+1),NULL,NULL);
  723.     UpdateWindow(hWnd);
  724.     return(CurrentPosition);
  725.     }
  726.     return(CurrentPosition+Spacing);
  727. }
  728.  
  729.  
  730. /****************************************************************************
  731.  
  732.     FUNCTION: SQLTestProcessResults(HWND)
  733.  
  734.     PURPOSE:  If a valid dbprocess is present process all results from pending
  735.           select statement, output each field to client area.  Whenever
  736.           a new line is written to client area it is checked to see if
  737.           the client area needs to be scrolled.
  738.  
  739.     PARAMETERS: hWnd - Handle to the window.
  740.  
  741.     RETURN:    Returns the Y coordinate for the next line of text.
  742.  
  743.     COMMENTS:
  744.         This function will bind the fields in the select statement
  745.             to local variables, format an output string then
  746.             write that string to the client area via TextOut.
  747.  
  748. ****************************************************************************/
  749. BOOL SqlTestProcessResults(HWND hWnd)
  750. {
  751.     HDC hDC;                /* display context         */
  752.     TEXTMETRIC tm;            /* text metric structure     */
  753.     char szOutputString[81];        /* general output string     */
  754.     int Y;                /* Y coordinate for text output  */
  755.     int Spacing;            /* Spacing between lines     */
  756.     unsigned short i;
  757.  
  758.     errhWnd = hWnd;
  759.     hDC = GetDC(hWnd);            /* get display context         */
  760.     GetTextMetrics(hDC, (LPTEXTMETRIC)(&tm)); /* get font info         */
  761.     Spacing = tm.tmExternalLeading + tm.tmHeight; /* set up spacing     */
  762.     Y = 1;                /* start at line 1         */
  763.     if(dbproc == (PDBPROCESS)NULL)    /* if process null, no results     */
  764.     {
  765.     ReleaseDC(hWnd,hDC);        /* free resources and return     */
  766.     return(TRUE);
  767.     }
  768.     SendMessage(hWnd,WM_ERASEBKGND,hDC,0L); /* always erase background     */
  769.     UpdateWindow(hWnd);            /* force painting of window     */
  770.     DBLOCKLIB();            /* lock down library         */
  771.  
  772.      /* Convert from OEM to ansi*/
  773.      for (i=0 ; i < NROWS ; i++)
  774.      {
  775.        if (pstat[i] & FTC_SUCCEED)
  776.        {        /* Print only if fetch succeeded */
  777.      OemToAnsi((LPSTR)(au_lname[i]), (LPSTR)(au_lname[i]));
  778.      OemToAnsi((LPSTR)(au_fname[i]), (LPSTR)(au_fname[i]));
  779.      OemToAnsi((LPSTR)(au_city[i]), (LPSTR)(au_city[i]));
  780.      OemToAnsi((LPSTR)(au_state[i]), (LPSTR)(au_state[i]));
  781.  
  782.      /* here we format each field and write it to client */
  783.      /* area checking to see if the client area needs to */
  784.      /* be scrolled after each line is written         */
  785.      sprintf(szOutputString,"Last Name: %s",au_lname[i]);
  786.      TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  787.      Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  788.  
  789.      sprintf(szOutputString,"First Name: %s",au_fname[i]);
  790.      TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  791.      Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  792.  
  793.      sprintf(szOutputString,"City:  %s, State:  %s",au_city[i], au_state[i]);
  794.      TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  795.      Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  796.  
  797.      Y = CheckForScroll(hWnd,Y,Spacing,0); /* add extra line     */
  798.                               /* after each results */
  799.       }
  800.       else if (pstat[i] & FTC_MISSING)
  801.       {
  802.      sprintf(szOutputString,"Row %d is missing.", i+1);
  803.      TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  804.      Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  805.  
  806.      Y = CheckForScroll(hWnd,Y,Spacing,0); /* add extra line     */
  807.       }
  808.     }
  809.  
  810.     DBUNLOCKLIB();                /* unlock library       */
  811.     ReleaseDC(hWnd,hDC);            /* free resource       */
  812.     return(TRUE);
  813. }
  814.  
  815.  
  816. /****************************************************************************
  817.  
  818.     FUNCTION: dbwinMessageHandler(PDBPROCESS, DBINT, DBSMALLINT, DBSMALLINT,
  819.             LPSTR)
  820.  
  821.     PURPOSE:  When the Data Server returns a message to dblib this function
  822.           will be called to process that message.  This function is
  823.           installed into dblib via MakeProcInstance.  It must be declared
  824.           as a FAR cdecl function, not as a FAR PASCAL function, unlike
  825.           other call back routines, as dblib conducts all of it's calls
  826.           in the cdecl fashion.  You must return 0 to dblib.
  827.  
  828.     RETURN:    Return 0
  829.  
  830.     COMMENTS:
  831.  
  832. ****************************************************************************/
  833.  
  834. int FAR dbwinMessageHandler(PDBPROCESS dbproc, DBINT msgno, DBSMALLINT msgstate, DBSMALLINT severity, LPSTR msgtext)
  835. {
  836.     MessageBox(errhWnd,msgtext,(LPSTR)"SQL DataServer Message",MB_OK);
  837.     return(0);
  838. }
  839.  
  840.  
  841. /****************************************************************************
  842.  
  843.     FUNCTION: dbwinErrorHandler(PDBPROCESS, int, int, int, LPSTR, LPSTR)
  844.  
  845.     PURPOSE:  When dblib returns an error message to the application this
  846.           function will be called to process that error.  This function is
  847.           installed into dblib via MakeProcInstance.  It must be declared
  848.           as a FAR cdecl function, not as a FAR PASCAL function, unlike
  849.           other call back routines, as dblib conducts all of it's calls
  850.           in the cdecl fashion.  You must return either INT_CANCEL,
  851.           INT_CONTINUE, or INT_EXIT to dblib.
  852.  
  853.     RETURN:    Return continuation code.
  854.  
  855.     COMMENTS:
  856.  
  857. ****************************************************************************/
  858.  
  859. int FAR dbwinErrorHandler(PDBPROCESS dbproc, int severity, int errno, int oserr, LPSTR dberrstr, LPSTR oserrstr)
  860. {
  861.     MessageBox(errhWnd,dberrstr,(LPSTR)"DB-LIBRARY error",MB_ICONHAND | MB_OK);
  862.  
  863.     if (oserr != DBNOERR)    /* os error    */
  864.     {
  865.     MessageBox(errhWnd,oserrstr,(LPSTR)"Operating-System error",MB_ICONHAND | MB_OK);
  866.     }
  867.  
  868.     return(INT_CANCEL);    /* cancel command */
  869. }
  870.