home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c10 / extdat / extdatview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  6.6 KB  |  294 lines

  1. // extdatView.cpp : implementation of the CExtdatView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "extdat.h"
  6.  
  7. #include "extdatDoc.h"
  8. #include "extdatView.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CExtdatView
  18.  
  19. IMPLEMENT_DYNCREATE(CExtdatView, CView)
  20.  
  21. BEGIN_MESSAGE_MAP(CExtdatView, CView)
  22.     //{{AFX_MSG_MAP(CExtdatView)
  23.     ON_COMMAND(ID_EXTERNAL_LINK_DBASE, OnExternalLinkDBase)
  24.     ON_COMMAND(ID_EXTERNAL_LINK_CDTEXT, OnExternalCDtext)
  25.     ON_COMMAND(ID_EXTERNAL_LINK_FWTEXT, OnExternalLinkFWtext)
  26.     ON_COMMAND(ID_EXTERNAL_OPEN_FWTEXT, OnExternalOpenFWText)
  27.     ON_COMMAND(ID_EXTERNAL_OPEN_CDTEXT, OnExternalOpenCDText)
  28.     ON_COMMAND(ID_EXTERNAL_OPEN_DBASE, OnExternalOpenDBase)
  29.     //}}AFX_MSG_MAP
  30. END_MESSAGE_MAP()
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CExtdatView construction/destruction
  34.  
  35. CExtdatView::CExtdatView()
  36. {
  37.     // TODO: add construction code here
  38.  
  39. }
  40.  
  41. CExtdatView::~CExtdatView()
  42. {
  43. }
  44.  
  45. BOOL CExtdatView::PreCreateWindow(CREATESTRUCT& cs)
  46. {
  47.     // TODO: Modify the Window class or styles here by modifying
  48.     //  the CREATESTRUCT cs
  49.  
  50.     return CView::PreCreateWindow(cs);
  51. }
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CExtdatView drawing
  55.  
  56. void CExtdatView::OnDraw(CDC* pDC)
  57. {
  58.     CExtdatDoc* pDoc = GetDocument();
  59.     ASSERT_VALID(pDoc);
  60.  
  61.     // TODO: add draw code for native data here
  62. }
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CExtdatView diagnostics
  66.  
  67. #ifdef _DEBUG
  68. void CExtdatView::AssertValid() const
  69. {
  70.     CView::AssertValid();
  71. }
  72.  
  73. void CExtdatView::Dump(CDumpContext& dc) const
  74. {
  75.     CView::Dump(dc);
  76. }
  77.  
  78. CExtdatDoc* CExtdatView::GetDocument() // non-debug version is inline
  79. {
  80.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CExtdatDoc)));
  81.     return (CExtdatDoc*)m_pDocument;
  82. }
  83. #endif //_DEBUG
  84.  
  85. CString CExtdatView::GetDefaultDBName()
  86. {
  87.     CFileStatus status;
  88.     CString strFileName= _T("..\\..\\States.mdb");
  89.     if( !CFile::GetStatus( strFileName, status ) )
  90.     {
  91.         strFileName= _T("..\\States.mdb");
  92.         if( !CFile::GetStatus( strFileName, status ) )
  93.         {
  94.             strFileName= _T("");
  95.         }
  96.     }
  97.     return strFileName;
  98. }
  99. CString CExtdatView::GetDefaultDirectory()
  100. {
  101.     CString filename= GetDefaultDBName();
  102.     // Remove the file name, keep the path w/o trailing '\'
  103.     int idx= filename.ReverseFind('\\');
  104.     if (idx == -1)
  105.         return _T("");
  106.     return filename.Left(idx);
  107. }
  108. CString CExtdatView::GetISAMName()
  109. {
  110.     return CString("Availabl");
  111. }
  112. CString CExtdatView::GetFWTextName()
  113. {
  114.     return CString("PetList.txt");
  115. }
  116. CString CExtdatView::GetCDTextName()
  117. {
  118.     return CString("Names.txt");
  119. }
  120. /////////////////////////////////////////////////////////////////////////////
  121. // CExtdatView message handlers
  122.  
  123.  
  124. ///      ISAM DATABASE
  125. void CExtdatView::OnExternalLinkDBase() 
  126. {
  127.  
  128.     // Open a database to link from
  129.     CDaoDatabase db;
  130.     db.Open(GetDefaultDBName());
  131.  
  132.     // Create a tabeldef
  133.     CDaoTableDef tbl(&db);
  134.     // Specify a friendly name, source table name and directory.
  135.     CString TableName("Available States");
  136.     tbl.Create(TableName);
  137.     tbl.SetSourceTableName(GetISAMName());
  138.     CString strConn;
  139.     strConn.Format("dBASE III;DATABASE=%s", GetDefaultDirectory());
  140.     tbl.SetConnect(strConn);    // for ODBC _T() required.
  141.  
  142.     // Try to delete any existing table with this name.
  143.     // This will throw an exception if no table exists,
  144.     //    which is OK.
  145.     try        
  146.     {
  147.         db.DeleteTableDef(TableName);
  148.     } 
  149.     catch (CDaoException* e) 
  150.     {
  151.         e->Delete();
  152.     }
  153.     tbl.Append();
  154.  
  155. }
  156.  
  157. void CExtdatView::OnExternalOpenDBase() 
  158. {
  159.     CString dbPath = GetDefaultDirectory();
  160.     CString dbDBaseName= GetISAMName();
  161.  
  162.     CDaoDatabase db;
  163.     db.Open(dbPath, FALSE, FALSE, "dBASE III;");
  164.  
  165.     CDaoRecordset rs(&db);
  166.     rs.Open(dbOpenTable,dbDBaseName);
  167.     while (!rs.IsEOF()) 
  168.     {
  169.         // process record ...
  170.         COleVariant field0;
  171.         rs.GetFieldValue(0, field0);
  172.         TRACE("field 0: %s\n", field0.pbVal);
  173.  
  174.         rs.MoveNext();
  175.     }
  176. }
  177.  
  178. // Character-delimited TEXT FILE
  179. void CExtdatView::OnExternalCDtext() 
  180. {
  181.  
  182.     // Open a database to link from
  183.     CDaoDatabase db;
  184.     db.Open(GetDefaultDBName());
  185.  
  186.     // Create a tabeldef
  187.     CDaoTableDef tbl(&db);
  188.     // Specify a friendly name, source table name and directory.
  189.     CString TableName("Names");
  190.     tbl.Create(TableName);
  191.     tbl.SetSourceTableName(GetCDTextName());
  192.     CString strConn;
  193.     strConn.Format("Text;DATABASE=%s", GetDefaultDirectory());
  194.     tbl.SetConnect(strConn);    // for ODBC _T() required around string.
  195.  
  196.     // Try to delete any existing table with this name.
  197.     // This will throw an exception if no table exists,
  198.     //    which is OK.
  199.     try        
  200.     {
  201.         db.DeleteTableDef(TableName);
  202.     } 
  203.     catch (CDaoException* e) 
  204.     {
  205.         e->Delete();
  206.     }
  207.     
  208.     tbl.Append();
  209.     
  210. }
  211. void CExtdatView::OnExternalOpenCDText() 
  212. {
  213.     // Open a text file as a dynaset recordset.
  214.     CString dbPath = GetDefaultDirectory();
  215.     CString dbTextFileName= GetCDTextName();
  216.     CString sql;
  217.     sql.Format("SELECT * FROM %s", dbTextFileName);
  218.  
  219.     CDaoDatabase db;
  220.     db.Open(dbPath, FALSE, FALSE, "Text;");
  221.  
  222.     CDaoRecordset rs(&db);
  223.     rs.Open(dbOpenDynaset,sql);
  224.     while (!rs.IsEOF()) 
  225.     {
  226.         // process record ...
  227.         COleVariant field0;
  228.         rs.GetFieldValue(0, field0);
  229.         TRACE("field0: %s\n", field0.pbVal);
  230.  
  231.         rs.MoveNext();
  232.     }
  233.     
  234. }
  235.  
  236. // Fixed width field TEXT FILE
  237. void CExtdatView::OnExternalLinkFWtext() 
  238. {
  239.     // Open a database to link from
  240.     CDaoDatabase db;
  241.     db.Open(GetDefaultDBName());
  242.  
  243.     // Create a tabeldef
  244.     CDaoTableDef tbl(&db);
  245.     // Specify a friendly name, source table name and directory.
  246.     CString TableName("Pets");
  247.     tbl.Create(TableName);
  248.     tbl.SetSourceTableName(GetFWTextName());
  249.     CString strConn;
  250.     strConn.Format("Text;DATABASE=%s", GetDefaultDirectory());
  251.     tbl.SetConnect(strConn);    // for ODBC _T() required around string.
  252.  
  253.     // Try to delete any existing table with this name.
  254.     // This will throw an exception if no table exists,
  255.     //    which is OK.
  256.     try        
  257.     {
  258.         db.DeleteTableDef(TableName);
  259.     } 
  260.     catch (CDaoException* e) 
  261.     {
  262.         e->Delete();
  263.     }
  264.  
  265.  
  266.     tbl.Append();
  267.     
  268. }
  269.  
  270. void CExtdatView::OnExternalOpenFWText() 
  271. {
  272.     // Open a text file as a dynaset recordset.
  273.     CString dbPath = GetDefaultDirectory();
  274.     CString dbTextFileName= GetFWTextName();
  275.     CString sql;
  276.     sql.Format("SELECT * FROM %s", dbTextFileName);
  277.  
  278.     CDaoDatabase db;
  279.     db.Open(dbPath, FALSE, FALSE, "Text;");
  280.  
  281.     CDaoRecordset rs(&db);
  282.     rs.Open(dbOpenDynaset,sql);
  283.     while (!rs.IsEOF()) 
  284.     {
  285.         // process record ...
  286.         COleVariant field0;
  287.         rs.GetFieldValue(0, field0);
  288.         TRACE("field0: %s\n", field0.pbVal);
  289.  
  290.         rs.MoveNext();
  291.     }
  292.     
  293. }
  294.