home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / mslang / dbeng / dbengsam.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-11  |  15.7 KB  |  576 lines

  1. // dbengsam.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "dbeng.h"
  6. #include "dbengsam.h" 
  7. #include "dbeng2.h"
  8.  
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char BASED_CODE THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CDBENGSAMP dialog
  16.  
  17. CDBENGSAMP::CDBENGSAMP(CWnd* pParent /*=NULL*/)
  18.     : CDialog(CDBENGSAMP::IDD, pParent)
  19. {
  20.     //{{AFX_DATA_INIT(CDBENGSAMP)
  21.     m_dbengine = NULL;
  22.     m_id = "";
  23.     m_lastname = "";
  24.     m_firstname = "";
  25.     m_mi = "";
  26.     //}}AFX_DATA_INIT
  27. }
  28.  
  29. void CDBENGSAMP::DoDataExchange(CDataExchange* pDX)
  30. {
  31.     CDialog::DoDataExchange(pDX);
  32.     //{{AFX_DATA_MAP(CDBENGSAMP)
  33.     DDX_VBControl(pDX, IDC_DBENGINE1, m_dbengine);
  34.     DDX_Text(pDX, IDC_EDIT1, m_id);
  35.     DDX_Text(pDX, IDC_EDIT2, m_lastname);
  36.     DDX_Text(pDX, IDC_EDIT3, m_firstname);
  37.     DDX_Text(pDX, IDC_EDIT4, m_mi);
  38.     //}}AFX_DATA_MAP
  39. }
  40.  
  41. BEGIN_MESSAGE_MAP(CDBENGSAMP, CDialog)
  42.     //{{AFX_MSG_MAP(CDBENGSAMP)
  43.     ON_BN_CLICKED(IDC_BUTTON1, OnCreateTable)
  44.     ON_BN_CLICKED(IDC_BUTTON2, OnCreateIndex)
  45.     ON_BN_CLICKED(IDC_BUTTON3, OnOpenTable)
  46.     ON_BN_CLICKED(IDC_BUTTON4, OnCloseTable)
  47.     ON_BN_CLICKED(IDC_BUTTON5, OnTop)
  48.     ON_BN_CLICKED(IDC_BUTTON6, OnBottom)
  49.     ON_BN_CLICKED(IDC_BUTTON7, OnNext)
  50.     ON_BN_CLICKED(IDC_BUTTON8, OnPrevious)
  51.     ON_BN_CLICKED(IDC_BUTTON9, OnInsert)
  52.     ON_BN_CLICKED(IDC_BUTTON10, OnUpdate)
  53.     ON_BN_CLICKED(IDC_BUTTON11, OnDelete)
  54.     ON_BN_CLICKED(IDC_BUTTON12, OnClear)
  55.     ON_BN_CLICKED(IDC_BUTTON13, OnHide)
  56.     //}}AFX_MSG_MAP
  57. END_MESSAGE_MAP()
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CDBENGSAMP message handlers
  61.  
  62. void CDBENGSAMP::OnCreateTable()
  63. {   
  64.     // This function creates the C:\TEST database table with the following
  65.     // Table structure:
  66.     //
  67.     //        Field            Field Type
  68.     //    -----------------------------------
  69.     //        ID                    N
  70.     //        Last Name            A30
  71.     //        First Name            A20
  72.     //        Middle Initial        A1
  73.     
  74.     long result;
  75.     
  76.     m_dbengine->SetStrProperty("TableName","C:\\TEST");
  77.     m_dbengine->SetNumProperty("NFields",4);
  78.     m_dbengine->SetStrProperty("TableFieldNames","ID,Last Name,First Name,Middle Initial");
  79.     m_dbengine->SetStrProperty("TableFieldTypes","N,A30,A20,A1");
  80.     m_dbengine->SetNumProperty("Action",CreateTable);
  81.     result = m_dbengine->GetNumProperty("Reaction");                  
  82.     
  83.     // If the database table could not be created display meesage box.
  84.     if(result != 0)
  85.         DisplayError();
  86.         
  87.     
  88. }
  89.  
  90. void CDBENGSAMP::OnCreateIndex()
  91. {
  92.     // This function will create a primary index for our C:\TEST
  93.     // database table. The primary index will contain one key field
  94.     // ID. Once the primary index is created our database table will
  95.     // have the following structure:    
  96.     //
  97.     //        Field            Field Type
  98.     //    -----------------------------------
  99.     //        ID                    N*
  100.     //        Last Name            A30
  101.     //        First Name            A20
  102.     //        Middle Initial        A1
  103.     //
  104.     // Where key fields are marked with an *.
  105.     
  106.     long result;
  107.                                                                
  108.     m_dbengine->SetNumProperty("IndexNFields",1);
  109.     m_dbengine->SetNumProperty("IndexId",1);
  110.     m_dbengine->SetNumProperty("IndexType", Primary);
  111.                                                                    
  112.     m_dbengine->SetNumProperty("Action",CreateIndex);
  113.     result = m_dbengine->GetNumProperty("Reaction");                  
  114.     
  115.     // If the database table could not be created display meesage box.
  116.     if(result != 0)
  117.         DisplayError();                                                              
  118.     
  119.     
  120. }
  121.  
  122. void CDBENGSAMP::OnOpenTable()
  123. {
  124.     // This function opens the C:\TEST database table and fills
  125.     // in the form with the information present in the first record.
  126.     
  127.     long result;
  128.     
  129.     
  130.       // Open the database table                     
  131.       m_dbengine->SetNumProperty("Action",OpenTable);
  132.       result = m_dbengine->GetNumProperty("Reaction");
  133.       if(result != 0)
  134.           DisplayError();
  135.       else
  136.        
  137.           FillForm();
  138.     
  139. }
  140.  
  141. void CDBENGSAMP::OnCloseTable()
  142. {   
  143.     long result;                      
  144.     // This function closes the database table.
  145.     
  146.     m_dbengine->SetNumProperty("Action",CloseTable);
  147.     result = m_dbengine->GetNumProperty("Reaction");
  148.     if(result != 0)
  149.         DisplayError();
  150.     
  151. }
  152.  
  153. void CDBENGSAMP::OnTop()
  154. {
  155.     long result;
  156.     
  157.     
  158.       // Move to the first record in the database table then fill the form
  159.       
  160.       m_dbengine->SetNumProperty("Action",FirstRecord);
  161.       result = m_dbengine->GetNumProperty("Reaction"); 
  162.       if(result != 0)
  163.           DisplayError();
  164.       else
  165.           FillForm();
  166.     
  167. }
  168.  
  169. void CDBENGSAMP::OnBottom()
  170. {
  171.     long result;
  172.     
  173.     
  174.       // Move to the last record in the database table then fill the form
  175.       
  176.       m_dbengine->SetNumProperty("Action",LastRecord);
  177.       result = m_dbengine->GetNumProperty("Reaction");
  178.       if(result != 0)
  179.           DisplayError();
  180.       else 
  181.           FillForm();
  182.     
  183. }
  184.  
  185. void CDBENGSAMP::OnNext()
  186. {
  187.     long result;
  188.     
  189.     
  190.       // Move to the next record in the database table then fill the form
  191.       
  192.       m_dbengine->SetNumProperty("Action",NextRecord);
  193.       result = m_dbengine->GetNumProperty("Reaction");
  194.       if(result != 0) 
  195.           DisplayError();
  196.       else
  197.           FillForm();
  198.     
  199. }
  200.  
  201. void CDBENGSAMP::OnPrevious()
  202. {
  203.     long result;
  204.     
  205.     
  206.       // Move to the previous record in the database table then fill the form
  207.       
  208.       m_dbengine->SetNumProperty("Action",PreviousRecord);
  209.       result = m_dbengine->GetNumProperty("Reaction");
  210.       if(result != 0)
  211.           DisplayError();
  212.       else 
  213.           FillForm();
  214.     
  215. }
  216.  
  217. void CDBENGSAMP::OnInsert()
  218. {                   
  219.     long result;
  220.     
  221.     // Insert the form data into the database table
  222.     
  223.     PutForm();    // Place form data into the current record
  224.     m_dbengine->SetNumProperty("Action",InsertRecord);
  225.     result = m_dbengine->GetNumProperty("Reaction");
  226.       if(result != 0)
  227.           DisplayError();
  228.     
  229. }
  230.  
  231. void CDBENGSAMP::OnUpdate()
  232. {
  233.     long result;
  234.     
  235.     // Updates the current record with data present on the form
  236.     
  237.     PutForm();    // Place form data into the current record
  238.     m_dbengine->SetNumProperty("Action",UpdateRecord);
  239.     result = m_dbengine->GetNumProperty("Reaction");
  240.     if(result != 0)
  241.           DisplayError();
  242.     
  243. }
  244.  
  245. void CDBENGSAMP::OnDelete()
  246. {               
  247.     long result;
  248.     
  249.     // Delete the current record
  250.     m_dbengine->SetNumProperty("Action",DeleteRecord);
  251.     result = m_dbengine->GetNumProperty("Reaction");
  252.     if(result != 0)
  253.           DisplayError();
  254.     else      
  255.         FillForm();
  256.     
  257. }
  258.  
  259. void CDBENGSAMP::OnClear()
  260. {            
  261.  
  262.     // This function just clears the form (Dialog Box) fields.
  263.     // No database operations are performed here.
  264.     
  265.     CWnd *pCtlMyString = GetDlgItem(IDC_EDIT1);
  266.     
  267.     
  268.     m_id = "";                                
  269.     pCtlMyString->SetWindowText(m_id);
  270.     m_lastname = "";
  271.     pCtlMyString = GetDlgItem(IDC_EDIT2);
  272.     pCtlMyString->SetWindowText(m_lastname);
  273.     m_firstname = "";                       
  274.     pCtlMyString = GetDlgItem(IDC_EDIT3);
  275.     pCtlMyString->SetWindowText(m_firstname);
  276.     m_mi = "";                               
  277.     pCtlMyString = GetDlgItem(IDC_EDIT4);
  278.     pCtlMyString->SetWindowText(m_mi);
  279.     
  280. }
  281.  
  282. void CDBENGSAMP::OnHide()
  283. {
  284.     // Hide the DBEngine Custom Control by setting the Visible property False
  285.     
  286.     m_dbengine->SetNumProperty("Visible",0);
  287. }
  288.  
  289.  
  290. void CDBENGSAMP::FillForm()
  291. {                       
  292.  
  293.     // This function fills the form (Dialog Box) with the information
  294.     // contained in the DBEngine Custom Control's current record.
  295.     
  296.   CWnd *pCtlMyString = GetDlgItem(IDC_EDIT1);
  297.   long result;
  298.       
  299.       // Get the first record in the database table 
  300.       // Before we can GetField(s) we must first GetRecord.
  301.       m_dbengine->SetNumProperty("Action",GetRecord);
  302.       
  303.       // Get the ID field value
  304.       m_dbengine->SetStrProperty("FieldName","ID");  // field of interest
  305.       m_dbengine->SetNumProperty("Action",GetField);    // Get the field value
  306.       result = m_dbengine->GetNumProperty("Reaction"); // GetField OK?
  307.       if(result != 0)
  308.           DisplayError();        // display error
  309.       else {               // if no error
  310.           m_id = m_dbengine->GetStrProperty("FieldValue"); // assign field value to id variable.
  311.           pCtlMyString = GetDlgItem(IDC_EDIT1);    // Then place it on the form
  312.           pCtlMyString->SetWindowText(m_id);        // (Dialog Box).
  313.        }
  314.          
  315.       // Repeat the process for each and every field on the form (Dialog Box).
  316.          
  317.       // Get Last Name field value
  318.       m_dbengine->SetStrProperty("FieldName","Last Name");
  319.       m_dbengine->SetNumProperty("Action",GetField);
  320.       result = m_dbengine->GetNumProperty("Reaction");
  321.       if(result != 0)
  322.           DisplayError();
  323.       else {              // if no error
  324.           m_lastname = m_dbengine->GetStrProperty("FieldValue");
  325.           pCtlMyString = GetDlgItem(IDC_EDIT2);
  326.           pCtlMyString->SetWindowText(m_lastname);
  327.       }
  328.       
  329.       // Get First Name field value
  330.       m_dbengine->SetStrProperty("FieldName","First Name");
  331.       m_dbengine->SetNumProperty("Action",GetField);
  332.       result = m_dbengine->GetNumProperty("Reaction");
  333.       if(result != 0)
  334.           DisplayError();
  335.       else {                // if no error
  336.           m_firstname = m_dbengine->GetStrProperty("FieldValue");
  337.           pCtlMyString = GetDlgItem(IDC_EDIT3);
  338.           pCtlMyString->SetWindowText(m_firstname);
  339.       }                                   
  340.       
  341.       // Get Middle Initial (MI) field value
  342.       m_dbengine->SetStrProperty("FieldName","Middle Initial");
  343.       m_dbengine->SetNumProperty("Action",GetField);
  344.       result = m_dbengine->GetNumProperty("Reaction");
  345.       if(result != 0)
  346.           DisplayError();
  347.       else {                // if no error
  348.           m_mi = m_dbengine->GetStrProperty("FieldValue");
  349.           pCtlMyString = GetDlgItem(IDC_EDIT4);
  350.           pCtlMyString->SetWindowText(m_mi);
  351.       }
  352. }
  353.  
  354. void CDBENGSAMP::PutForm()
  355. {                   
  356.  
  357.     // This function transfers the information contained in the form 
  358.     // (Dialog Box) to the DBEngine Custom Control's current record.
  359.     // Information will not be placed in the database table until a
  360.     // InsertRecord or UpdateRecord Action is performed.
  361.      
  362.     long result;
  363.     
  364.     UpdateData(TRUE);    // Transfer data from form to string variables 
  365.       
  366.       // Place the ID field into the current record   
  367.       m_dbengine->SetStrProperty("FieldName","ID");
  368.       m_dbengine->SetStrProperty("FieldValue",m_id);
  369.       m_dbengine->SetNumProperty("Action",PutField);
  370.       result = m_dbengine->GetNumProperty("Reaction");
  371.       if(result != 0)
  372.               DisplayError();
  373.               
  374.       // Place the Last Name field into the current record   
  375.       m_dbengine->SetStrProperty("FieldName","Last Name");
  376.       m_dbengine->SetStrProperty("FieldValue",m_lastname);
  377.       m_dbengine->SetNumProperty("Action",PutField);    
  378.       result = m_dbengine->GetNumProperty("Reaction");
  379.       if(result != 0)
  380.               DisplayError();
  381.               
  382.       // Place the First Name field into the current record   
  383.       m_dbengine->SetStrProperty("FieldName","First Name");
  384.       m_dbengine->SetStrProperty("FieldValue",m_firstname);
  385.       m_dbengine->SetNumProperty("Action",PutField);     
  386.       result = m_dbengine->GetNumProperty("Reaction");
  387.       if(result != 0)
  388.               DisplayError();
  389.               
  390.       // Place the MI field into the current record   
  391.       m_dbengine->SetStrProperty("FieldName","Middle Initial");
  392.       m_dbengine->SetStrProperty("FieldValue",m_mi);
  393.       m_dbengine->SetNumProperty("Action",PutField);
  394.       result = m_dbengine->GetNumProperty("Reaction");
  395.       if(result != 0)
  396.               DisplayError();
  397. }
  398.  
  399.  void CDBENGSAMP::DisplayError() 
  400.  {
  401.              
  402.     long result;
  403.     int temp;
  404.     
  405.     // This error handling routine is not complete.
  406.     // It provides an example of how such a routine can be
  407.     // constructed.
  408.     // 
  409.     // If you continue to get an Unknown error! message when
  410.     // you run this sample program, set a breakpoint at the
  411.     // first line of code below and look at the value in the
  412.     // result variable (the DBEngine Reaction property setting)
  413.     // and cross index the error code with the error code listing
  414.     // in the Appendix of the DBEngine 2.0 Custom Control Reference Guide.
  415.             
  416.     result = m_dbengine->GetNumProperty("Reaction");
  417.     switch(result) {
  418.     
  419.         case 1:
  420.               temp = AfxMessageBox("Drive not ready!", MB_OK,0);
  421.               break;
  422.                      
  423.           case 2:
  424.               temp = AfxMessageBox("Directory not found!", MB_OK,0);
  425.               break;
  426.                     
  427.           case 3:
  428.               temp = AfxMessageBox("File is busy!", MB_OK,0);
  429.               break;
  430.           
  431.           case 4:
  432.               temp = AfxMessageBox("File is locked!", MB_OK,0);
  433.               break;
  434.           
  435.           case 5:
  436.               temp = AfxMessageBox("File not found!", MB_OK,0);
  437.               break;
  438.           
  439.           case 6:
  440.               temp = AfxMessageBox("Table is corrupt!", MB_OK,0);
  441.               break;
  442.           
  443.           case 7:
  444.               temp = AfxMessageBox("Primary index is corrupt!", MB_OK,0);
  445.               break;        
  446.               
  447.           case 8:
  448.               temp = AfxMessageBox("Primary index is out of date!", MB_OK,0);
  449.               break;
  450.               
  451.           case 9:
  452.               temp = AfxMessageBox("Record is locked!", MB_OK,0);
  453.               break;                                
  454.               
  455.           case 10:
  456.               temp = AfxMessageBox("Sharing violation!", MB_OK,0);
  457.               break;
  458.               
  459.           case 11:
  460.               temp = AfxMessageBox("Sharing violation!", MB_OK,0);
  461.               break;
  462.               
  463.           case 12:
  464.               temp = AfxMessageBox("No access to directory!", MB_OK,0);
  465.               break;
  466.               
  467.           case 13:
  468.               temp = AfxMessageBox("Sort order for index differs from table!", MB_OK,0);
  469.               break;
  470.               
  471.           case 14:
  472.               temp = AfxMessageBox("Single user but directory is shared!", MB_OK,0);
  473.               break;                                                   
  474.               
  475.           case 15:
  476.               temp = AfxMessageBox("Multiple PARADOX.NET files!", MB_OK,0);
  477.               break;
  478.               
  479.           case 21:
  480.               temp = AfxMessageBox("Insufficient password rights!", MB_OK,0);
  481.               break;
  482.               
  483.           case 22:
  484.               temp = AfxMessageBox("Table is write protected!", MB_OK,0);
  485.               break;
  486.               
  487.           case 30:
  488.               temp = AfxMessageBox("Data type mismatch!", MB_OK,0);
  489.               break;
  490.               
  491.           case 31:
  492.               temp = AfxMessageBox("Argument is out of range!", MB_OK,0);
  493.               break;
  494.               
  495.           case 33:
  496.               temp = AfxMessageBox("Invalid argument!", MB_OK,0);
  497.               break;
  498.               
  499.           case 40:
  500.               temp = AfxMessageBox("Not enough memory to complete operation!", MB_OK,0);
  501.               break;
  502.               
  503.           case 41:
  504.               temp = AfxMessageBox("Not enough disk space to complete operation!", MB_OK,0);
  505.               break;
  506.               
  507.           case 50:
  508.               temp = AfxMessageBox("Another user deleted record!", MB_OK,0);
  509.               break;
  510.               
  511.           case 70:
  512.               temp = AfxMessageBox("No more file handles!", MB_OK,0);
  513.               break;                                    
  514.               
  515.           case 72:
  516.               temp = AfxMessageBox("No more table handles!", MB_OK,0);
  517.               break;
  518.               
  519.           case 73:
  520.               temp = AfxMessageBox("Invalid date!", MB_OK,0);
  521.               break;
  522.               
  523.           case 74:
  524.               temp = AfxMessageBox("Invalid field name!", MB_OK,0);
  525.               break;
  526.               
  527.           case 78:
  528.               temp = AfxMessageBox("Engine not initialized!", MB_OK,0);
  529.               break;
  530.               
  531.           case 97:
  532.               temp = AfxMessageBox("Key violation!", MB_OK,0);
  533.               break;
  534.                                                  
  535.         case 98:
  536.               temp = AfxMessageBox("Could not login on network!", MB_OK,0);
  537.               break;                                               
  538.           
  539.           case 99:
  540.               temp = AfxMessageBox("Invalid table name!", MB_OK,0);
  541.               break;
  542.               
  543.           case 101:
  544.               temp = AfxMessageBox("End of table!", MB_OK,0);
  545.               break;
  546.               
  547.           case 102:
  548.               temp = AfxMessageBox("Start of table!", MB_OK,0);
  549.               break;
  550.           
  551.           case 103:
  552.               temp = AfxMessageBox("No more record handles available!", MB_OK,0);
  553.               break;
  554.               
  555.           case 105:
  556.               temp = AfxMessageBox("Operation on empty table!", MB_OK,0);
  557.               break;
  558.                     
  559.           case 120:
  560.               temp = AfxMessageBox("Table not found!", MB_OK,0);
  561.               break;
  562.               
  563.           case 134:
  564.               temp = AfxMessageBox("Can't lock PARADOX.NET - is SHARE.EXE loaded?", MB_OK,0);
  565.               break;
  566.           
  567.           case 126:
  568.               temp = AfxMessageBox("General hardware error!", MB_OK,0);
  569.               break;
  570.               
  571.           default:
  572.               temp = AfxMessageBox("Unknown error!", MB_OK,0);
  573.       }
  574.  
  575.  
  576.  }