The EX31A Recordset Example

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:

  1. Copy the Student Registration database to your hard disk. You can find the file stdreg32.mdb in the \Samples\VC98\Mfc\Database\Stdreg directory on the Visual C++ MSDN CD-ROM. Copy it to the new project directory on your hard disk, and make sure the copy does not have its read-only attribute set.

  2. Run the ODBC Data Source Administrator to install the Student Registration data source. Click the ODBC icon in the Windows Control Panel. The Visual C++ Setup program should have already installed the required ODBC drivers on your hard disk. If you are running Windows 95, click the Drivers button to see whether the Microsoft Access driver is available. If you're running Windows 98, click the Drivers tab to see whether the Microsoft Access driver is available. (If the Microsoft Access driver is not available, rerun Visual C++ Setup.) Click the Add button (in Windows 98, the Add button is on the User DSN tab), choose Microsoft Access Driver in the Add Data Source dialog box (in Windows 98, select the Microsoft Access Driver in the Create New Data Source dialog box and click the Finish button), and fill in the ODBC Microsoft Access 97 Setup dialog box as shown here.

    click to view at full size.

    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.

  • Run AppWizard to produce \vcpp32\ex31a\. Specify an SDI application (Step 1 dialog box) with CScrollView as the view's class type (Step 6 dialog box). Select the Header Files Only option from the AppWizard Step 2 dialog box, as shown here.

    click to view at full size.

  • Use ClassWizard to create the CEx31aSet recordset class. Choose New from the Add Class menu, and then fill in the New Class dialog box as shown here.

  • Select the Student Registration database's Student table for the CEx31aSet class. When you click the OK button in the New Class dialog box, ClassWizard displays the Database Options dialog box. Select the Student Registration data source, and select the Dynaset option as shown here.

    After you select the data source, ClassWizard prompts you to select a table. Select Student, as shown here.

  • Examine the data members that ClassWizard generates. Click on the Member Variables tab for the newly generated CEx31aSet class. ClassWizard should have generated data members based on student column names, as shown here.

    click to view at full size.

  • Declare an embedded recordset object in ex31aDoc.h. Add the following public data member in the CEx31aDoc class declaration:

    CEx31aSet m_ex31aSet;

  • Edit the ex31aDoc.cpp file. Add the line

    #include "ex31aSet.h"

    just before the line

    #include "ex31aDoc.h"

  • Declare a recordset pointer in ex31aView.h. Add the following private data member in the CEx31aView class declaration:

    CEx31aSet* m_pSet;

  • Edit the OnDraw and OnInitialUpdate functions in ex31aView.cpp. Add the following boldface code:

    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"

  • Edit the ex31a.cpp file. Add the line

    #include "ex31aSet.h"

    just before the line

    #include "ex31aDoc.h"

  • Build and test the EX31A application. Does the resulting screen look like the one shown here?

    click to view at full size.

    Adding ODBC Capability to an MFC Application

    If 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 resources

    add the line

    #include "afxdb.rc"       // database resources


  • Programming Visual C++
    Advanced 3ds max 5 Modeling & Animating
    ISBN: 1572318570
    EAN: 2147483647
    Year: 1997
    Pages: 331
    Authors: Boris Kulagin

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net