You can use AppWizard to generate a complete forms-oriented database application, and that's what the Enroll tutorial is all about. If customers or users wanted a straightforward business database application like that, however, they probably wouldn't call in a Visual C++ programmer; instead, they might use a less technical tool, such as Microsoft Access. Visual C++ and the MFC ODBC classes are more appropriate for a complex application that might have an incidental need for database access. You can also use the classes to make your own general-purpose database query tool.
The EX31A program isolates the database access code from user interface code so that you can see how to add ODBC database capability to any MFC application. You'll be using ClassWizard to generate a CRecordset class, but you won't be using the CRecordView class that AppWizard generates when you ask for a database view application.
The EX31A application is fairly simple. It displays the rows from the student database table in a scrolling view, as shown in the screen at the end of this section. The student table is part of the Student Registration (Microsoft Access 97) sample database that's included with Visual C++.
Here are the steps for building the EX31A example:
Set the database to point to stdreg32.mdb using the Select button. Finally, click the OK button.
If you are using Microsoft Windows NT version 4.0, click on the ODBC icon in the Windows Control Panel and then click on the ODBC Drivers tab to see whether the Microsoft Access Driver is available. On the User DSN tab, click the Add button, choose Microsoft Access Driver in the Create New Data Source dialog box, click the Finish button, and then fill in the dialog box as shown above.
After you select the data source, ClassWizard prompts you to select a table. Select Student, as shown here.
CEx31aSet m_ex31aSet;
#include "ex31aSet.h"
just before the line
#include "ex31aDoc.h"
CEx31aSet* m_pSet;
void CEx31aView::OnDraw(CDC* pDC) { TEXTMETRIC tm; pDC->GetTextMetrics(&tm); int nLineHeight=tm.tmHeight+tm.tmExternalLeading; CPoint pText(0,0); int y = 0; CString str; if (m_pSet->IsBOF()) { // detects empty recordset return; } m_pSet->MoveFirst(); // fails if recordset is empty while (!m_pSet->IsEOF()) { str.Format("%ld", m_pSet->m_StudentID); pDC->TextOut(pText.x, pText.y, str); pDC->TextOut(pText.x+1000, pText.y, m_pSet->m_Name); str.Format("%d", m_pSet->m_GradYear); pDC->TextOut(pText.x+4000, pText.y, str); m_pSet->MoveNext(); pText.y -= nLineHeight; } } void CEx31aView::OnInitialUpdate() { CScrollView::OnInitialUpdate(); CSize sizeTotal(8000, 10500); SetScrollSizes(MM_HIENGLISH, sizeTotal); m_pSet = &GetDocument()->m_ex31aSet; // Remember that documents/views are reused in SDI applications! if (m_pSet->IsOpen()) { m_pSet->Close(); } m_pSet->Open(); }
Also in ex31aView.cpp, add the line
#include "ex31aSet.h"
just before the line
#include "ex31aDoc.h"
#include "ex31aSet.h"
just before the line
#include "ex31aDoc.h"
Adding ODBC Capability to an MFC ApplicationIf you need to add ODBC capability to an existing MFC application, make the following changes to the project:
Add the following line at the end of StdAfx.h: #include <afxdb.h>Edit the RC file in text mode. After the line "#include ""afxprint.rc"" // printing print preview resources\r\n"add the line
"#include ""afxdb.rc"" // database resources\r\n"And after the line
#include "afxprint.rc" // printing print preview resourcesadd the line
#include "afxdb.rc" // database resources