home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / 32SNIPIT.PAK / DBIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  3.9 KB  |  118 lines

  1. // BDE32 3.x - (C) Copyright 1996 by Borland International
  2.  
  3. // dbio.c
  4. #include "snipit.h"
  5.  
  6. #define NUMDB 5
  7. #define MSG_LEN 30
  8.  
  9. //=====================================================================
  10. //  Function:
  11. //          DatabaseIOSample();
  12. //
  13. //  Description:
  14. //          This example shows how to open a number of databases and get
  15. //          information about those databases.
  16. //=====================================================================
  17. void
  18. DatabaseIOSample (void)
  19. {
  20.     hDBIDb      hDb[NUMDB];         // Array of handles to databases
  21.     hDBICur     hCur = 0;           // Handle to the table
  22.     DBIResult   rslt;               // Value returned from IDAPI functions
  23.     DBIPATH     szDir;              // String to hold the directory
  24.     CHAR        szMessage[MSG_LEN]; // String to hold messages
  25.     UINT16      uOpenedDatabases;   // Count of the opened databases
  26.     UINT16      uLoop;              // Loop variable
  27.     DBDesc      DbDesc;             // Information about the databases
  28.  
  29.     Screen("*** Database Manipulation Example ***\r\n");
  30.  
  31.     BREAK_IN_DEBUGGER();
  32.  
  33.     Screen("   Initializing IDAPI...\r\n");
  34.     rslt = DbiInit(NULL);
  35.     if (ChkRslt(rslt, "Init") != DBIERR_NONE)
  36.     {
  37.         Screen("\r\n*** End of Example ***");
  38.         return;
  39.     }
  40.  
  41.     // Open up to iNUMDB databases
  42.     for (uOpenedDatabases=0; uOpenedDatabases < NUMDB; uOpenedDatabases++)
  43.     {
  44.         Screen("   Open database #%i", uOpenedDatabases + 1);
  45.         rslt = DbiOpenDatabase(NULL, "STANDARD", dbiREADWRITE, dbiOPENSHARED,
  46.                                NULL, NULL, NULL, NULL, &hDb[uOpenedDatabases]);
  47.         if (rslt != DBIERR_NONE)
  48.         {
  49.             // Know how to deal with running out of database handles.
  50.             if (rslt == DBIERR_DBLIMIT)
  51.             {
  52.                 break;
  53.             }
  54.  
  55.             ChkRslt(rslt, "OpenDatabase");
  56.  
  57.             rslt = DbiExit();
  58.             ChkRslt(rslt, "Exit");
  59.  
  60.             Screen("\r\n*** End of Example ***");
  61.             return;
  62.         }
  63.     }
  64.  
  65.     Screen("\r\n   Get the default directory for the first database...");
  66.     rslt = DbiGetDirectory(hDb[0], FALSE, szDir);
  67.     ChkRslt(rslt, "GetDirectory");
  68.  
  69.     Screen("   The working directory: %s", szDir);
  70.  
  71.     Screen("   Change the directory which the first database uses...");
  72.     rslt = DbiSetDirectory(hDb[0], (pCHAR)szTblDirectory);
  73.     ChkRslt(rslt, "SetDirectory");
  74.  
  75.     // Display information about the available databases.
  76.     // Note that this example reads the data from the schema table
  77.     // directly into a structure defined in IDAPI.H.  This should
  78.     // only be done with schema tables.
  79.     rslt = DbiOpenDatabaseList(&hCur);
  80.     if (ChkRslt(rslt, "OpenDatabaseList") == DBIERR_NONE)
  81.     {
  82.         Screen("\r\n   Display information about the available databases:");
  83.         while ((rslt = DbiGetNextRecord(hCur, dbiNOLOCK, (pBYTE)&DbDesc, NULL))
  84.                == DBIERR_NONE)
  85.         {
  86.             Screen("\r\n   Logical Name: %s", DbDesc.szName);
  87.             Screen("   Description: %s", DbDesc.szText);
  88.             Screen("   Physical name/path: %s", DbDesc.szPhyName);
  89.             Screen("   Database type: %s", DbDesc.szDbType);
  90.         }
  91.  
  92.         // Trap for unexpected errors....
  93.         if (rslt != DBIERR_EOF)
  94.         {
  95.             ChkRslt(rslt, "GetNextRecord");
  96.         }
  97.     }
  98.  
  99.     // Place an empty line in the output edit control
  100.     Screen("");
  101.  
  102.     // Clean up and return
  103.     for (uLoop = 0; uLoop < uOpenedDatabases; uLoop++)
  104.     {
  105.         Screen("   Close database #%i", uLoop + 1);
  106.         wsprintf(szMessage, "CloseDatabase #%i", uLoop + 1);
  107.  
  108.         rslt = DbiCloseDatabase(&hDb[uLoop]);
  109.         ChkRslt(rslt, szMessage);
  110.     }
  111.  
  112.     Screen("\r\n   Clean up IDAPI");
  113.     rslt = DbiExit();
  114.     ChkRslt(rslt, "Exit");
  115.  
  116.     Screen("\r\n*** End of Example ***");
  117. }
  118.