ENROLL Sample: A Sample MFC Database Application

Switch View :
ScriptFree
Visual C++ MFC Samples
ENROLL Sample: A Sample MFC Database Application

The ENROLL sample is an example of an MFC database application. ENROLL illustrates the following:

  • Code created by the application wizard database option.
  • CRecordset and CRecordView classes.
  • Using MFC database classes in conjunction with the framework's document/view architecture.
  • Adding, updating, and deleting database records.
  • Handling database exceptions.
  • Using multiple CRecordView-derived classes.
  • Switching views in a frame window.
  • Using update hints and CDocument::UpdateAllViews to coordinate multiple record views.

Building and Running the Sample

Note   To run the ENROLL application, you must first set up the Student Registration Database as described in "Setting Up the Student Registration Data Source."

To build and run the ENROLL sample

  1. Open the solution Enroll.sln.
  2. On the Build menu, click Build Solution.
  3. Click the Start button to run the Enroll application.

The user interface for ENROLL includes a Form menu and a Course form. Use the Form menu to toggle between the Sections form and the Course form. When you add or delete a course in the Course form, the application automatically reflects the change in the list of courses it displays in the dialog box in the Section form.

Setting Up the Student Registration Data Source

Before you run the ENROLL application, you must set up the Student Registration database and register it as an ODBC data source. Choose a database format for which you have the corresponding database management system (DBMS) and 32-bit ODBC driver. Microsoft ships with Visual C++ the 32-bit ODBC drivers for most standard database formats, including: SQL Server, Access, Paradox, dBase, Excel, and Oracle.

  • If you want to use MFC database support for SQL Server, you need the SQL Server product in addition to the ODBC driver for SQL Server that is provided with Visual C++.
  • If you want to use other database formats, you need the DBMS as well as the ODBC driver. See Installing Additional 32-bit ODBC Drivers for Your DBMS, later in this topic, for more information.
  • If you want to use the Microsoft Access database format, you need only the 32-bit Microsoft Access ODBC driver to create a database schema. This driver is installed automatically when you run a Typical setup. This is an exception to the requirements listed above. You may find it helpful, however, to use Microsoft Access itself in conjunction with MFC database support, as it will facilitate working interactively with your database schema and data.

To set up the student registration database, you must:

  1. Specify a database.
  2. Register the database with ODBC. If you are using a DBMS other than the prebuilt Microsoft Access StdReg32.mdb database file, you need to add tables to your database so that it matches the Student Registration database schema. Additionally, you may need to install drivers other than those that Visual C++ Setup installs for you. In this case, perform the next two steps:
  3. Use the StdReg.exe tool to add tables to the Student Registration database.
  4. Install additional 32-bit ODBC drivers for your DBMS. This step is not necessary if your DBMS uses the dBase, Access, or SQL Server drivers, as Setup installs them automatically when you choose the Typical installation option. Perform this step to install additional ODBC drivers, including those supplied by Visual C++: Paradox, Excel, or Oracle.

Specify a Database

The easiest way to supply a database for Enroll is to use the StdReg32.mdb Microsoft Access database file, included in the MFC Samples directory for this purpose. Alternately, you can create your own database, as described below.

StdReg32.mdb already contains the tables and records used in the sample. If you use this file, you do not need to use the StdReg.exe tool to add any tables.

To create your own database

  • Create a new database schema using the database administration capability of your DBMS.

    Depending on the type of DBMS, you might create the new database on a server that is different from the computer where you will be doing MFC database development. In either case, you need to use the StdReg.exe tool to add tables to the new database.

Register the New Database with ODBC

Register the new database with the ODBC data source name "Student Registration." This data source name (DSN) is referred to by the Enroll application. You must register the database even if you are using the pre-built StdReg32.mdb Microsoft Access database file.

You can register the data source using ODBC Administrator from the Control Panel, or by running StdReg.exe.

Note   The STDREG tool is provided for use with Enroll as a convenient method for registering data sources with ODBC and for populating databases with the appropriate tables and data. Normally, you will use ODBC Administrator from the Control Panel to register your data sources with the appropriate tables and data if you are not going to use the pre-built StdReg32.mdb database.

The following two procedures describe how to register the data source if you are using the Microsoft Access ODBC driver.

  • If you are using Microsoft Access, make sure you have copied the StdReg32.mdb database to your hard drive before you perform the following procedures. To copy StdReg32.mdb, go to the STDREG sample and click the link to copy the STDREG project files.
  • If you are using another driver, the basic procedure will vary somewhat. For more information, refer to the ODBC SDK topic, Adding Data Sources.

To register the data source using StdReg.exe

  • If you are using the default StdReg32.mdb (Access) database as shipped in the StdReg folder, this is the easiest method of setting up the Student Registration database. See "Building and Running the Sample" in STDREG for detailed instructions on using StdReg.exe.

To register the data source using ODBC Data Source Administrator

  1. Open Control Panel, Administrative Tools, and select Data Sources (ODBC). The ODBC Data Source Administrator appears.
  2. Click the User DSN tab, if it is not already selected.
  3. Click the Add button.

    The Create New Data Source dialog box appears.

  4. From the list, choose the driver you want to use with your database, in this case, Microsoft Access Driver (*.mdb), and then click Finish.

    The ODBC Setup dialog box specific to the driver you indicated appears.

  5. In the Data Source Name box, type Student Registration.
  6. Optionally, in the Description box, enter a description for the database.
  7. In the Database group box, click Select, and using the Select Database dialog box, navigate to the location of StdReg32.mdb. Click OK to select the database.
  8. Click OK to exit the ODBC Setup dialog box, and click Close to exit the ODBC Data Source Administrator dialog box.

Use StdReg.exe to Add Tables

If you are using a DBMS other than the prebuilt Microsoft Access StdReg32.mdb database file, use the STDREG tool to add tables to the Student Registration database. This tool creates the Student Registration tables listed in the following table and also adds records to the newly created tables for use as test data by the Enroll application.

Tables in the Student Registration Database

Table name Contents Column list
Course Think of each record as an entry in a course catalog. Example: the MATH101 course. CourseID*

CourseTitle

Hours

Section A section record is a specific offering of a course at a specific time. For example, MATH101 may have many sections. SectionNo*

CourseID*

InstructorID

Schedule

RoomNo

Student A record for each student at the school. StudentID*

Name

GradYear

Enrollment A record for each student in a particular section of a course. For a given student, there is an enrollment record for each course the student is taking. CourseID*

SectionNo*

StudentID*

Grade

Instructor A record for each instructor at the school. InstructorID*

Name

RoomNo

* Indicates the column (or columns) that comprise the table's primary key.

The Dynabind_Section table is used in the Dynabind sample, but not in the Enroll tutorial.

To add tables using StdReg.exe

  1. If necessary, start StdReg.exe, and then click the Initialize Data option.

    Depending on the type of database you are using, you may need to respond to a login dialog box, such as the SQL Server Login dialog box.

  2. After logging in to the Student Registration data source, respond to a series of three Enter SQL Column Syntax dialog boxes.

    After you respond to three successive Enter SQL Column Syntax dialog boxes, STDREG creates the tables in the new database.

  3. When STDREG has completed this task, click Exit.

    You can rerun the STDREG tool at any time to remove and re-create the tables in the Student Registration data source.

For more information about using StdReg.exe, see the STDREG sample.

Installing Additional 32-Bit ODBC Drivers for Your DBMS

You need only install the ODBC driver for your DBMS once, and you can use it with more than one data source. If you choose the Typical setup option, Setup provides MFC Database Support and installs the ODBC drivers for Access, dBase, and SQL Server automatically. You can install the additional drivers provided with Visual C++ (Text, Paradox, Excel, and Oracle) by running Setup again, as described in the following procedure.

If you want to install other drivers not shipped with Visual C++, refer to the documentation that came with your driver. You can also review the ODBC SDK documentation, in particular, the Installing Drivers topic.

To install additional drivers provided with Visual C++

  1. Run Setup, and under Installation Options, select Custom.
  2. Click Next, and in the Microsoft Visual C++ Setup dialog box, clear each option except Database Options.
  3. Highlight Database Options and then click the Details button.
  4. In the Database Options dialog box, highlight Microsoft ODBC Drivers, and again click the Details button.

    If you do not want to install DAO database support, clear the Microsoft Data Access Objects check box.

  5. In the Microsoft ODBC Drivers dialog box, check the boxes next to any additional drivers you want to install.
  6. Click OK, and then click the Next button to start the installation.

Keywords

The ENROLL sample demonstrates the following keywords:

AfxMessageBox; CComboBox::AddString; CComboBox::DeleteString; CComboBox::FindStringExact; CComboBox::GetCurSel; CComboBox::GetLBText; CComboBox::ResetContent; CComboBox::SetCurSel; CDatabase::IsOpen; CDialog::DoModal; CDocument::OnNewDocument; CEdit::SetReadOnly; CFieldExchange::SetFieldType; CFrameWnd::Create; CFrameWnd::DockControlBar; CFrameWnd::EnableDocking; CFrameWnd::GetActiveView; CFrameWnd::RecalcLayout; CFrameWnd::SetActiveView; CFrameWnd::rectDefault; CObject::AssertValid; CObject::Dump; CObject::GetRuntimeClass; CObject::IsKindOf; CRecordView::OnGetRecordset; CRecordView::OnMove; CRecordset::AddNew; CRecordset::Delete; CRecordset::DoFieldExchange; CRecordset::GetDefaultSQL; CRecordset::IsBOF; CRecordset::IsEOF; CRecordset::IsOpen; CRecordset::MoveLast; CRecordset::MoveNext; CRecordset::Open; CRecordset::Requery; CRecordset::SetFieldNull; CRecordset::Update; CView::DoPreparePrinting; CView::GetDocument; CView::OnBeginPrinting; CView::OnEndPrinting; CView::OnInitialUpdate; CView::OnPreparePrinting; CView::OnUpdate; CWinApp::AddDocTemplate; CWinApp::InitInstance; CWinApp::LoadStdProfileSettings; CWnd::Create; CWnd::DoDataExchange; CWnd::GetDlgItem; CWnd::GetWindowText; CWnd::OnCreate; CWnd::PreCreateWindow; CWnd::SetDlgCtrlID; CWnd::ShowWindow; CWnd::UpdateData

Note   Some samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task.

See Also

MFC Samples