home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a025 / 11.ddi / SQLTEST3.C@ / SQLTEST3.bin
Encoding:
Text File  |  1992-09-15  |  25.5 KB  |  729 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: SqlTest3.c
  4.          Copyright (C) 1988-1990 Microsoft Corp.
  5.  
  6.     PURPOSE: SqlTest sample Windows applications
  7.  
  8.     FUNCTIONS:
  9.  
  10.     WinMain() - calls initialization function, processes message loop
  11.     SqlTestInit() - initializes window data and registers window
  12.     SqlTestWndProc() - processes messages
  13.     AboutSQL() - processes messages for "About" dialog box
  14.     SelectSQL() - processes input of author name
  15.     ConnectSQL() - processes input of server name and connects to server
  16.  
  17.     COMMENTS:
  18.  
  19.     Windows can have several copies of your application running at the
  20.     same time.  The variable hInst keeps track of which instance this
  21.     application is so that processing will be to the correct window.
  22.  
  23.     You only need to initialize the application once.  After it is
  24.     initialized, all other copies of the application will use the same
  25.     window class, and do not need to be separately initialized.
  26.  
  27. ****************************************************************************/
  28.  
  29. #include "windows.h"            /* required for all Windows applications*/
  30. #define DBMSWIN                /* needed to define environment         */
  31. #include "stdio.h"
  32. #include "string.h"
  33. #include "sqlfront.h"            /* standard dblib include file        */
  34. #include "sqldb.h"            /* standard dblib include file        */
  35. #include "sqltest3.h"            /* specific to this program            */
  36.  
  37. DBPROCESS *dbproc = (DBPROCESS *)NULL;
  38.                     /* dbprocess pointer for dblib connection*/
  39. HANDLE hInst;                /* current instance                */
  40. HWND ghWnd;                /* global window handle for handlers    */
  41. HWND errhWnd;                /* global window handle for current error*/
  42. /****************************************************************************
  43.  
  44.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  45.  
  46.     PURPOSE: calls initialization function, processes message loop
  47.  
  48.     COMMENTS:
  49.  
  50.     This will initialize the window class if it is the first time this
  51.     application is run.  It then creates the window, and processes the
  52.     message loop until a PostQuitMessage is received.  It exits the
  53.     application by returning the value passed by the PostQuitMessage.
  54.  
  55. ****************************************************************************/
  56.  
  57. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  58. HANDLE hInstance;                 /* current instance         */
  59. HANDLE hPrevInstance;                 /* previous instance         */
  60. LPSTR lpCmdLine;                 /* command line             */
  61. int nCmdShow;                     /* show-window type (open/icon) */
  62. {
  63.     HWND hWnd;                     /* window handle             */
  64.     MSG msg;                     /* message                 */
  65.  
  66.  
  67.     if (!hPrevInstance)            /* Has application been initialized? */
  68.     if (!SqlTestInit(hInstance))
  69.         return (NULL);        /* Exits if unable to initialize     */
  70.  
  71.     hInst = hInstance;            /* Saves the current instance         */
  72.  
  73.     hWnd = CreateWindow("SQL Test",          /* window class         */
  74.     "SQL Server Sample Windows Application",  /* window name         */
  75.     WS_OVERLAPPEDWINDOW,              /* window style         */
  76.     CW_USEDEFAULT,                  /* x position             */
  77.     CW_USEDEFAULT,                  /* y position             */
  78.     CW_USEDEFAULT,                  /* width             */
  79.     CW_USEDEFAULT,                  /* height             */
  80.     NULL,                      /* parent handle         */
  81.     NULL,                      /* menu or child ID         */
  82.     hInstance,                  /* instance             */
  83.     NULL);                      /* additional info         */
  84.  
  85.     if (!hWnd)                      /* Was the window created? */
  86.     return (NULL);
  87.  
  88.     ghWnd = hWnd;                  /* set global handle         */
  89.     errhWnd = hWnd;
  90.  
  91.     ShowWindow(hWnd, nCmdShow);              /* Shows the window         */
  92.     UpdateWindow(hWnd);                  /* Sends WM_PAINT message  */
  93.  
  94.     while (GetMessage(&msg,       /* message structure                 */
  95.         NULL,           /* handle of window receiving the message */
  96.         NULL,           /* lowest message to examine             */
  97.         NULL))           /* highest message to examine         */
  98.     {
  99.     TranslateMessage(&msg);       /* Translates virtual key codes         */
  100.     DispatchMessage(&msg);       /* Dispatches message to window         */
  101.     }
  102.     return (msg.wParam);       /* Returns the value from PostQuitMessage */
  103. }
  104.  
  105.  
  106. /****************************************************************************
  107.  
  108.     FUNCTION: SqlTestInit(HANDLE)
  109.  
  110.     PURPOSE: Initializes window data and registers window class
  111.  
  112.     COMMENTS:
  113.  
  114.     Sets up a structure to register the window class.  Structure includes
  115.     such information as what function will process messages, what cursor
  116.     and icon to use, etc.
  117.  
  118.  
  119. ****************************************************************************/
  120.  
  121. BOOL SqlTestInit(hInstance)
  122. HANDLE hInstance;                   /* current instance         */
  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, WORD, LONG)
  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, message, wParam, lParam)
  175. HWND hWnd;                  /* window handle             */
  176. unsigned message;              /* type of message             */
  177. WORD wParam;                  /* additional information         */
  178. LONG lParam;                  /* additional information         */
  179. {
  180.     FARPROC lpProcAbout;          /* pointer to the "About" function */
  181.     FARPROC lpProcSQL;              /* pointer to the Select/Connect   */
  182.                       /* functions                  */
  183.     HMENU hMenu;              /* handle to the System menu         */
  184.     static FARPROC lpdbwinMessageHandler; /* pointer to message handler         */
  185.     static FARPROC lpdbwinErrorHandler;   /* pointer to error handler         */
  186.  
  187.     switch (message) {
  188.     case WM_SYSCOMMAND:        /* message: command from system menu */
  189.         if (wParam == ID_ABOUTSQL) {
  190.         lpProcAbout = MakeProcInstance(AboutSQL, hInst);
  191.  
  192.         DialogBox(hInst,         /* current instance         */
  193.             "ABOUTSQL",             /* resource to use         */
  194.             hWnd,             /* parent handle         */
  195.             lpProcAbout);         /* About() instance address */
  196.  
  197.         FreeProcInstance(lpProcAbout);
  198.         break;
  199.         }
  200.  
  201.         else                /* Lets Windows process it         */
  202.         return (DefWindowProc(hWnd, message, wParam, lParam));
  203.  
  204.     case WM_CREATE:                /* message: window being created */
  205.  
  206.         /* Get the handle of the System menu */
  207.  
  208.         hMenu = GetSystemMenu(hWnd, FALSE);
  209.  
  210.         /* Add a separator to the menu */
  211.  
  212.         ChangeMenu(hMenu,                  /* menu handle         */
  213.         NULL,                      /* menu item to change */
  214.         NULL,                      /* new menu item         */
  215.         NULL,                      /* menu identifier     */
  216.         MF_APPEND | MF_SEPARATOR);          /* type of change         */
  217.  
  218.         /* Add new menu item to the System menu */
  219.  
  220.         ChangeMenu(hMenu,                  /* menu handle         */
  221.         NULL,                      /* menu item to change */
  222.         "A&bout SQL Test...",              /* new menu item         */
  223.         ID_ABOUTSQL,                  /* menu identifier     */
  224.         MF_APPEND | MF_STRING);              /* type of change         */
  225.     
  226.                         /* Now make the message and error    */
  227.                     /* handler instances             */
  228.             dbinit();
  229.         lpdbwinMessageHandler =
  230.             MakeProcInstance((FARPROC)dbwinMessageHandler, hInst);
  231.         lpdbwinErrorHandler =
  232.             MakeProcInstance((FARPROC)dbwinErrorHandler, hInst);
  233.                     /* Install the instances into dblib */    
  234.         dbmsghandle(lpdbwinMessageHandler);
  235.         dberrhandle(lpdbwinErrorHandler);
  236.         break;
  237.     
  238.     case WM_COMMAND :            /* menu selections generate */
  239.                         /* the WM_COMMAND message   */    
  240.         switch(wParam)            /* menu in WORD parameter   */
  241.         {
  242.         case IDM_CONNECT :        /* connect to server        */
  243.             lpProcSQL = MakeProcInstance(ConnectSQL, hInst);
  244.  
  245.             DialogBox(hInst,        /* current instance         */
  246.             "CONNECT",         /* resource to use         */
  247.             hWnd,            /* parent handle         */
  248.             lpProcSQL);         /* ConnectSQL() instance address */
  249.  
  250.             FreeProcInstance(lpProcSQL);
  251.             break;
  252.     
  253.         case IDM_SELECT :        /* select an author        */
  254.             lpProcSQL = MakeProcInstance(SelectSQL, hInst);
  255.  
  256.             DialogBox(hInst,         /* current instance         */
  257.             "SELECT",         /* resource to use         */
  258.             hWnd,             /* parent handle         */
  259.             lpProcSQL);         /* About() instance address */
  260.  
  261.             FreeProcInstance(lpProcSQL);
  262.             break;
  263.         }
  264.         break;
  265.     
  266.     case WM_DBRESULTS :            /* a select has been issued */
  267.         SqlTestProcessResults(hWnd);    /* process results        */
  268.         break;
  269.  
  270.     case WM_DESTROY:          /* message: window being destroyed */
  271.         dbexit();              /* free any active dbprocesses     */
  272.         FreeProcInstance(lpdbwinMessageHandler);    /* release handlers  */
  273.         FreeProcInstance(lpdbwinErrorHandler);
  274.             dbwinexit();
  275.         PostQuitMessage(0);
  276.         break;
  277.  
  278.     default:              /* Passes it on if unproccessed    */
  279.         return (DefWindowProc(hWnd, message, wParam, lParam));
  280.     }
  281.     return (NULL);
  282. }
  283.  
  284.  
  285. /****************************************************************************
  286.  
  287.     FUNCTION: AboutSQL(HWND, unsigned, WORD, LONG)
  288.  
  289.     PURPOSE:  Processes messages for "AboutSQL" dialog box
  290.  
  291.     MESSAGES:
  292.  
  293.     WM_INITDIALOG - initialize dialog box
  294.     WM_COMMAND    - Input received
  295.  
  296.     COMMENTS:
  297.  
  298.     No initialization is needed for this particular dialog box, but TRUE
  299.     must be returned to Windows.
  300.  
  301.     Wait for user to click on "Ok" button, then close the dialog box.
  302.  
  303. ****************************************************************************/
  304.  
  305. BOOL FAR PASCAL AboutSQL(hDlg, message, wParam, lParam)
  306. HWND hDlg;
  307. unsigned message;
  308. WORD wParam;
  309. LONG lParam;
  310. {
  311.     switch (message) {
  312.     case WM_INITDIALOG:           /* message: initialize dialog box */
  313.         return (TRUE);
  314.  
  315.     case WM_COMMAND:              /* message: received a command */
  316.         if (wParam == IDOK) {          /* "OK" box selected?         */
  317.         EndDialog(hDlg, NULL);          /* Exits the dialog box         */
  318.         return (TRUE);
  319.         }
  320.         break;
  321.     }
  322.     return (FALSE);                  /* Didn't process a message    */
  323. }
  324. /****************************************************************************
  325.  
  326.     FUNCTION: SelectSQL(HWND, unsigned, WORD, LONG)
  327.  
  328.     PURPOSE:  Processes messages for "SelectSQL" 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.     Let user input into edit control the name of an author (the select
  341.     IS case sensitive).  When user presses OK, format the select statement
  342.     then send it to the server and execute it via dbsqlexec(). If the
  343.     dbsqlexec() SUCCEED's post a WM_DBRESULTS message so the results
  344.     may be retrieved and processed.
  345.  
  346.     Wait for user to click on "Ok" button, then close the dialog box.
  347.  
  348. ****************************************************************************/
  349.  
  350. BOOL FAR PASCAL SelectSQL(hDlg, message, wParam, lParam)
  351. HWND hDlg;
  352. unsigned message;
  353. WORD wParam;
  354. LONG lParam;
  355. {
  356.     char szSelectAuthor[41];          /* string for authors name        */
  357.     char szServerMess[45];          /* string for server response        */
  358.     char szAName[40];              /* format string for author        */
  359.     switch (message) {
  360.     case WM_INITDIALOG:           /* message: initialize dialog box */
  361.         SendDlgItemMessage(hDlg,       /* limit input to 40 characters   */
  362.         AUTHORNAME,EM_LIMITTEXT,40,0L);
  363.         return (TRUE);
  364.  
  365.     case WM_COMMAND:              /* message: received a command */
  366.         errhWnd = hDlg;
  367.         switch(wParam)
  368.         {
  369.         case IDOK :              /* "OK" box selected?         */
  370.             *szSelectAuthor = NULL;   /* Null author             */
  371.                 
  372.             GetDlgItemText(hDlg,AUTHORNAME, /* get input name         */
  373.             (LPSTR)szSelectAuthor,
  374.                 MAX_ANAME);
  375.             if(dbproc == (DBPROCESS *)NULL) /* if not a valid process*/
  376.             {
  377.                     /* No server to query        */
  378.             MessageBox(hDlg,
  379.                 "No SQL Server Connected to Query",
  380.                 "SQL Test",MB_ICONHAND | MB_OK);
  381.             }
  382.             else if(*szSelectAuthor != NULL) /* if a name exists */
  383.             {
  384.             DBLOCKLIB();        /* lock down the library */
  385.                             /* format the select statement */
  386.             dbcmd(dbproc,
  387.                 (LPSTR)"select au_id, au_lname,"
  388.                 "au_fname, phone, address, city, state, zip");
  389.             dbcmd(dbproc, (LPSTR)" from authors");
  390.             dbcmd(dbproc, (LPSTR)" where au_lname = ");
  391.             sprintf(szAName,"'%s'",szSelectAuthor);
  392.             dbcmd(dbproc,(LPSTR)szAName);
  393.             if(dbsqlexec(dbproc) == FAIL)
  394.             {
  395.                 sprintf(szServerMess,    /* error, not in db */
  396.                 "%s not found in database pubs",
  397.                     szSelectAuthor);
  398.                 MessageBox(hDlg,
  399.                     (LPSTR)szServerMess,(LPSTR)"SQL Test",
  400.                     MB_ICONHAND | MB_OK);
  401.             }
  402.             else    /* query SUCCEEDed so             */
  403.             {    /* post message to process results    */
  404.                 PostMessage(GetParent(hDlg),WM_DBRESULTS,0,0L);
  405.             }
  406.             DBUNLOCKLIB();        /* unlock library    */
  407.             }
  408.             EndDialog(hDlg, NULL);          /* Exits the dialog box         */
  409.             return (TRUE);
  410.             break;
  411.         case IDCANCEL :
  412.             EndDialog(hDlg, NULL);          /* cancelled select */
  413.             return(TRUE);
  414.             break;
  415.         
  416.         }
  417.         break;
  418.     }
  419.     return (FALSE);                  /* Didn't process a message    */
  420. }
  421. /****************************************************************************
  422.  
  423.     FUNCTION: ConnectSQL(HWND, unsigned, WORD, LONG)
  424.  
  425.     PURPOSE:  Processes messages for "Connect" dialog box
  426.  
  427.     MESSAGES:
  428.  
  429.     WM_INITDIALOG - initialize dialog box
  430.     WM_COMMAND    - Input received
  431.  
  432.     COMMENTS:
  433.  
  434.     No initialization is needed for this particular dialog box, but TRUE
  435.     must be returned to Windows.
  436.  
  437.     Wait for user to click on "Ok" button, then close the dialog box.
  438.  
  439. ****************************************************************************/
  440.  
  441. BOOL FAR PASCAL ConnectSQL(hDlg, message, wParam, lParam)
  442. HWND hDlg;
  443. unsigned message;
  444. WORD wParam;
  445. LONG lParam;
  446. {
  447.     char szSQLServer[31];
  448.     char szServerMess[81];
  449.     static LOGINREC *LoginRec;
  450.  
  451.     *szSQLServer = NULL;
  452.     switch (message) {
  453.     case WM_INITDIALOG:           /* message: initialize dialog box*/
  454.         SendDlgItemMessage(hDlg,       /* limit input to 30 characters  */
  455.         SQL_SERVER,EM_LIMITTEXT,30,0L);
  456.         return (TRUE);
  457.  
  458.     case WM_COMMAND:              /* message: received a command*/
  459.         errhWnd = hDlg;
  460.         switch(wParam)
  461.         {
  462.         case IDOK :              /* "OK" box selected?        */
  463.             GetDlgItemText(hDlg,SQL_SERVER,
  464.             (LPSTR)szSQLServer,
  465.                 MAX_SERVERNAME); /* get Server name */
  466.             if(*szSQLServer != NULL) /* was something input        */
  467.             {
  468.             DBLOCKLIB();        /* lock down library        */
  469.             if(dbproc != (DBPROCESS *)NULL) /* if an active     */
  470.                                 /* process close it */
  471.                 dbclose(dbproc);
  472.             if((LoginRec = dblogin()) != (LOGINREC *)NULL) /* get loginrec */
  473.             {
  474.                 DBSETLUSER(LoginRec,(char far *)"sa"); /* set user  */
  475.                     /* now open the connection to server */
  476.                 if((dbproc = dbopen(LoginRec,(LPSTR)szSQLServer))
  477.                      == (DBPROCESS *)NULL)
  478.                 {
  479.                     /* if NULL couldn't connect    */
  480.                 dbfreelogin(LoginRec);
  481.                 }
  482.                 else /* got connect so use the pubs database */
  483.                 {
  484.                 dbuse(dbproc,(LPSTR)"pubs");
  485.                 dbfreelogin(LoginRec);
  486.                 }
  487.             }
  488.             else /* memory allocation problem */
  489.                 MessageBox(hDlg, "Could not allocate Login Record","System Error", MB_ICONHAND | MB_OK);
  490.             DBUNLOCKLIB(); /* done unlock library    */
  491.             }
  492.             EndDialog(hDlg, NULL);          /* Exits the dialog box         */
  493.             return (TRUE);
  494.             break;
  495.         case IDCANCEL :
  496.             EndDialog(hDlg, NULL);
  497.             return(TRUE);
  498.             break;
  499.         
  500.         }
  501.         break;
  502.     }
  503.     return (FALSE);                  /* Didn't process a message    */
  504. }
  505.  
  506. /****************************************************************************
  507.  
  508.     FUNCTION: CheckForScroll(HWND, int, int, int)
  509.  
  510.     PURPOSE:  Check if next output line will be out of client area
  511.  
  512.     PARAMETERS: hWnd - Handle to the window.
  513.         CurrentPosition - Current y coordinate for the line of
  514.             text just written to the client area.
  515.         Spacing - The height of the line (including the space
  516.             separating lines) of the text just written.
  517.         Length - The length of the line just written in device
  518.             units.
  519.  
  520.     RETURN:    Returns the Y coordinate for the next line of text.
  521.  
  522.     COMMENTS:
  523.  
  524.     Will determine if the next line of text will be out of the client
  525.     area.  If so will scroll the window for the next line.  Also validates
  526.     the current line of text so that a WM_PAINT will not clear it.
  527.  
  528. ****************************************************************************/
  529. int CheckForScroll(hWnd,CurrentPosition,Spacing, Length)
  530. HWND hWnd;
  531. int CurrentPosition;
  532. int Spacing;
  533. int Length;
  534. {
  535.     RECT rect;                /* RECT structure for validation */
  536.     rect.top = CurrentPosition;     /* top of last line of text     */
  537.     rect.bottom = CurrentPosition+Spacing+1; /* bottom of last line     */
  538.     rect.left = 1;            /* left most column of line     */
  539.     rect.right = Length+1;        /* right most column of line     */
  540.     ValidateRect(hWnd,(LPRECT)&rect);   /* validate line so that it is   */
  541.                     /* not blanked on next paint     */
  542.         
  543.     GetClientRect(hWnd,(LPRECT)&rect);    /* get rect for current client   */
  544.     if(CurrentPosition + (Spacing*2) > rect.bottom) /* will line fit     */
  545.     {
  546.                     /* if not scroll window and      */
  547.                     /* update client window         */
  548.     ScrollWindow(hWnd,0,-(Spacing+1),NULL,NULL);
  549.     UpdateWindow(hWnd);
  550.     return(CurrentPosition);
  551.     }
  552.     return(CurrentPosition+Spacing);
  553. }
  554.  
  555. /****************************************************************************
  556.  
  557.     FUNCTION: SQLTestProcessResults(HWND)
  558.  
  559.     PURPOSE:  If a valid dbprocess is present process all results from pending
  560.           select statement, output each field to client area.  Whenever
  561.           a new line is written to client area it is checked to see if
  562.           the client area needs to be scrolled.
  563.  
  564.     PARAMETERS: hWnd - Handle to the window.
  565.  
  566.     RETURN:    Returns the Y coordinate for the next line of text.
  567.  
  568.     COMMENTS:
  569.         This function will bind the fields in the select statement
  570.             to local variables, format an output string then
  571.             write that string to the client area via TextOut.
  572.         It is called by the main message processing loop
  573.         SQLTestWndProc via the message WM_DBRESULTS.
  574.  
  575. ****************************************************************************/
  576. BOOL SqlTestProcessResults(hWnd)
  577. HWND hWnd;
  578. {
  579.     HDC hDC;                /* display context         */
  580.     TEXTMETRIC tm;            /* text metric structure     */
  581.     char szId[12];            /* Author ID for binding     */
  582.     char szLastName[41];        /* Author last name for binding     */
  583.     char szFirstName[21];        /* Author first name for binding */
  584.     char szPhone[13];            /* Author phone for binding     */
  585.     char szAddress[41];            /* Author address for binding     */
  586.     char szCity[21];            /* Author city for binding     */
  587.     char szState[3];            /* Author state for binding     */
  588.     char szZip[6];            /* Author zipcode for binding     */
  589.     char szOutputString[81];        /* general output string     */
  590.     RETCODE result_code;        /* results code from dbresults     */
  591.     int Y;                /* Y coordinate for text output  */
  592.     int Spacing;            /* Spacing between lines     */
  593.     errhWnd = hWnd;
  594.  
  595.     hDC = GetDC(hWnd);            /* get display context         */
  596.     GetTextMetrics(hDC, (LPTEXTMETRIC)&tm); /* get font info         */
  597.     Spacing = tm.tmExternalLeading + tm.tmHeight; /* set up spacing     */
  598.     Y = 1;                /* start at line 1         */
  599.     if(dbproc == (DBPROCESS *)NULL)    /* if process null, no results     */
  600.     {
  601.     ReleaseDC(hWnd,hDC);        /* free resources and return     */
  602.     return(TRUE);
  603.     }
  604.     SendMessage(hWnd,WM_ERASEBKGND,hDC,0L); /* always erase background     */
  605.     UpdateWindow(hWnd);            /* force painting of window     */
  606.     DBLOCKLIB();            /* lock down library         */
  607.  
  608.                     /* get all results from the query*/
  609.     while(((result_code = dbresults(dbproc)) != NO_MORE_RESULTS) && result_code != FAIL)
  610.     {
  611.     if(result_code == SUCCEED)    /* if results ready         */
  612.     {
  613.                     /* Bind all data of interest     */
  614.         dbbind(dbproc,1,NTBSTRINGBIND, 12L, (LPSTR)szId);
  615.         dbbind(dbproc,2,NTBSTRINGBIND, 41L, (LPSTR)szLastName);
  616.         dbbind(dbproc,3,NTBSTRINGBIND, 21L, (LPSTR)szFirstName);
  617.         dbbind(dbproc,4,NTBSTRINGBIND, 13L, (LPSTR)szPhone);
  618.         dbbind(dbproc,5,NTBSTRINGBIND, 41L, (LPSTR)szAddress);
  619.         dbbind(dbproc,6,NTBSTRINGBIND, 21L, (LPSTR)szCity);
  620.         dbbind(dbproc,7,NTBSTRINGBIND, 3L, (LPSTR)szState);
  621.         dbbind(dbproc,8,NTBSTRINGBIND, 6L, (LPSTR)szZip);
  622.         while(dbnextrow(dbproc) != NO_MORE_ROWS) /* get all rows     */
  623.         {
  624.             /* here we format each field and write it to client */
  625.             /* area checking to see if the client area needs to */
  626.             /* be scrolled after each line is written        */
  627.         sprintf(szOutputString,"Author ID: %s",szId);
  628.         TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  629.         Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  630.  
  631.         sprintf(szOutputString,"Last Name: %s",szLastName);
  632.         TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  633.         Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  634.  
  635.         sprintf(szOutputString,"Address:   %s",szAddress);
  636.         TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  637.         Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  638.  
  639.         sprintf(szOutputString,"City:      %s",szCity);
  640.         TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  641.         Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  642.  
  643.         sprintf(szOutputString,"State:     %s",szState);
  644.         TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  645.         Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  646.  
  647.         sprintf(szOutputString,"ZipCode:   %s",szZip);
  648.         TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  649.         Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  650.  
  651.         sprintf(szOutputString,"Telephone: %s",szPhone);
  652.         TextOut(hDC,1,Y,szOutputString,strlen(szOutputString));
  653.         Y = CheckForScroll(hWnd,Y,Spacing,strlen(szOutputString) * tm.tmMaxCharWidth);
  654.  
  655.         Y = CheckForScroll(hWnd,Y,Spacing,0); /* add extra line     */
  656.                               /* after each results */
  657.         }
  658.     }
  659.     }
  660.  
  661.     DBUNLOCKLIB();                /* unlock library       */
  662.     ReleaseDC(hWnd,hDC);            /* free resource       */
  663.     return(TRUE);
  664. }
  665. /****************************************************************************
  666.  
  667.     FUNCTION: dbwinMessageHandler(DBPROCESS *, DBINT, DBSMALLINT, DBSMALLINT,
  668.             LPSTR)
  669.  
  670.     PURPOSE:  When the Data Server returns a message to dblib this function
  671.           will be called to process that message.  This function is
  672.           installed into dblib via MakeProcInstance.  It must be declared
  673.           as a FAR cdecl function, not as a FAR PASCAL function, unlike
  674.           other call back routines, as dblib conducts all of it's calls
  675.           in the cdecl fashion.  You must return 0 to dblib.
  676.  
  677.     RETURN:    Return 0
  678.  
  679.     COMMENTS:
  680.  
  681. ****************************************************************************/
  682.  
  683. int FAR dbwinMessageHandler(dbproc, msgno, msgstate, severity, msgtext)
  684. DBPROCESS        *dbproc;
  685. DBINT            msgno;
  686. DBSMALLINT       msgstate;
  687. DBSMALLINT       severity;
  688. LPSTR            msgtext;
  689. {
  690.     MessageBox(errhWnd,msgtext,(LPSTR)"SQL DataServer Message",MB_OK);
  691.     return(0);
  692. }
  693.  
  694. /****************************************************************************
  695.  
  696.     FUNCTION: dbwinErrorHandler(DBPROCESS *, int, int, int, LPSTR, LPSTR)
  697.  
  698.     PURPOSE:  When dblib returns an error message to the application this
  699.           function will be called to process that error.  This function is
  700.           installed into dblib via MakeProcInstance.  It must be declared
  701.           as a FAR cdecl function, not as a FAR PASCAL function, unlike
  702.           other call back routines, as dblib conducts all of it's calls
  703.           in the cdecl fashion.  You must return either INT_CANCEL,
  704.           INT_CONTINUE, or INT_EXIT to dblib.
  705.  
  706.     RETURN:    Return continuation code.
  707.  
  708.     COMMENTS:
  709.  
  710. ****************************************************************************/
  711.  
  712. int FAR dbwinErrorHandler(dbproc, severity, errno, oserr, dberrstr, oserrstr)
  713. DBPROCESS *dbproc;
  714. int severity;
  715. int errno;
  716. int oserr;
  717. LPSTR dberrstr;
  718. LPSTR oserrstr;
  719. {
  720.     MessageBox(errhWnd,dberrstr,(LPSTR)"DB-LIBRARY error",MB_ICONHAND | MB_OK);
  721.  
  722.     if (oserr != DBNOERR)    /* os error    */
  723.     {
  724.     MessageBox(errhWnd,oserrstr,(LPSTR)"Operating-System error",MB_ICONHAND | MB_OK);
  725.     }
  726.  
  727.     return(INT_CANCEL);    /* cancel command */
  728. }
  729.