home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / database / daotable / database.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  5KB  |  202 lines

  1. // database.cpp -- MFC DAO Database specific functions
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. //
  14. // contains the functions that deal directly with DAODatabase
  15. // creation, opening and closing:
  16. //
  17. // void closeDatabase(CDaoDatabase **ppDatabase);
  18. //
  19. // int openDatabase(CDaoDatabase **ppDatabase, CString fileName,
  20. //                BOOL bReportNoOpen = TRUE);
  21. //
  22. // BOOL createDatabase(CDaoDatabase **ppDatabase, CString fileName,
  23. //                int dwOptions = 0);
  24.  
  25. #include "stdafx.h"
  26. #include "database.h"
  27.  
  28. // close the database and destruct it
  29. // IN/OUT:: ppDatabase--pointer to pointer to database to close and
  30. // destruct
  31. void closeDatabase(CDaoDatabase **ppDatabase)
  32. {
  33.     // only process if the database object exists
  34.     if (*ppDatabase != NULL)
  35.     {
  36.         if ((*ppDatabase)->IsOpen())
  37.             (*ppDatabase)->Close();
  38.  
  39.         // closing doesn't delete the object
  40.         delete *ppDatabase;
  41.         *ppDatabase = NULL;
  42.     }
  43. }
  44.  
  45. // open the database from a file (close if necessary)
  46. // IN/OUT: ppDatabase--pointer to pointer to database object
  47. // IN: fileName--name of .mdb file to open
  48. // IN: bReportNoOpen--TRUE by default, if true, if open fails
  49. //     because the specified MDB file doesn't exist, report that
  50. //     fact to the user.  Note: other errors are reported always
  51. // RETURN: 1 if success, 0 if non-fatal failure, -1 if fatal failure
  52. int openDatabase(CDaoDatabase **ppDatabase, CString fileName,
  53.                   BOOL bReportNoOpen /* = TRUE */)
  54. {
  55.     // initialize success indicator
  56.     int nReturnCode = 1;
  57.  
  58.     // close and delete if necessary
  59.     if (*ppDatabase != NULL)
  60.     {
  61.         if ((*ppDatabase)->IsOpen())
  62.             closeDatabase(ppDatabase);
  63.         delete *ppDatabase;
  64.     }
  65.  
  66.     // construct new database
  67.     *ppDatabase = new CDaoDatabase;
  68.  
  69.     // failed to allocate
  70.     if (ppDatabase == NULL)
  71.         return -1; // fatal error
  72.  
  73.     // now open the database object with error checking
  74.     try
  75.     {
  76.         (*ppDatabase)->Open(fileName);
  77.     }
  78.     catch (CDaoException *e)
  79.     {
  80.         // special case--couldn't find the file, so it may be because
  81.         // user specified a new file to open
  82.         if (e->m_pErrorInfo->m_lErrorCode == 3024)
  83.         {
  84.             if (bReportNoOpen)
  85.             {
  86.                 // create a message to display
  87.                 CString message = _T("Couldn't open database--Exception: ");
  88.                 message += e->m_pErrorInfo->m_strDescription;
  89.  
  90.                 // output status
  91.                 AfxMessageBox(message);
  92.             }
  93.  
  94.             // indicate failure but not fatal
  95.             nReturnCode = 0;
  96.         }
  97.         else // other type of DAO exception--always report
  98.         {
  99.             // create a message to display
  100.             CString message = _T("Couldn't open database--Exception: ");
  101.             message += e->m_pErrorInfo->m_strDescription;
  102.  
  103.             // output status
  104.             AfxMessageBox(message);
  105.  
  106.             // indicate fatal error
  107.             nReturnCode = -1;
  108.         }
  109.  
  110.         // not rethrowing, so delete exception
  111.         e->Delete();
  112.  
  113.         delete *ppDatabase;
  114.         *ppDatabase = NULL;
  115.     }
  116.     catch (CMemoryException *e)
  117.     {
  118.         // output status
  119.         AfxMessageBox(_T("Failed to open database--Memory exception thrown."));
  120.  
  121.         // not rethrowing, so delete exception
  122.         e->Delete();
  123.  
  124.         delete *ppDatabase;
  125.         *ppDatabase = NULL;
  126.  
  127.         // indicate fatal error
  128.         nReturnCode = -1;
  129.     }
  130.  
  131.     return nReturnCode;
  132. }
  133.  
  134. // create the database file (close any open database)
  135. // IN/OUT: ppDatabase--pointer to pointer to database object
  136. // IN: fileName--name of .mdb file to open
  137. // IN: dwOptions--info like version and encryption settings
  138. //     0 by default
  139. // RETURN: TRUE if success, FALSE if failure
  140. BOOL createDatabase(CDaoDatabase **ppDatabase, CString fileName,
  141.                   int dwOptions /* = 0 */)
  142. {
  143.     // initialize success indicator
  144.     BOOL bSuccess = TRUE;
  145.  
  146.     // close and delete if necessary
  147.     if (*ppDatabase != NULL)
  148.     {
  149.         if ((*ppDatabase)->IsOpen())
  150.             closeDatabase(ppDatabase);
  151.         delete *ppDatabase;
  152.     }
  153.  
  154.     // construct new database
  155.     *ppDatabase = new CDaoDatabase;
  156.  
  157.     // failed to allocate
  158.     if (ppDatabase == NULL)
  159.         return FALSE; // error
  160.  
  161.     // now create the database object with error checking
  162.     try
  163.     {
  164.         // default language specified
  165.         (*ppDatabase)->Create(fileName, dbLangGeneral, dwOptions);
  166.     }
  167.     catch (CDaoException *e)
  168.     {
  169.         // create a message to display
  170.         CString message = _T("Couldn't create database--Exception: ");
  171.         message += e->m_pErrorInfo->m_strDescription;
  172.  
  173.         // output status
  174.         AfxMessageBox(message);
  175.  
  176.         // not rethrowing, so delete exception
  177.         e->Delete();
  178.  
  179.         delete *ppDatabase;
  180.         *ppDatabase = NULL;
  181.  
  182.         // failure
  183.         bSuccess = FALSE;
  184.     }
  185.     catch (CMemoryException *e)
  186.     {
  187.         // output status
  188.         AfxMessageBox(_T("Failed to create database--Memory exception thrown."));
  189.  
  190.         // not rethrowing, so delete exception
  191.         e->Delete();
  192.  
  193.         delete *ppDatabase;
  194.         *ppDatabase = NULL;
  195.  
  196.         // failure
  197.         bSuccess = FALSE;
  198.     }
  199.  
  200.     return bSuccess;
  201. }
  202.