📄 ch22.htm
字号:
<A HREF="WfigC06.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC06.gif"><b>Fig. 22.6</b></A>
<P><I>Name your data source whatever you like.</I></P>
<ol start=5>
<li><P> Click the <U>S</U>elect button. The Select Database file selector appears. Use the selector to locate and select the DeptStore.mdb file (see Figure 22.7).</P>
</ol>
<A HREF="WfigC07.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC07.gif"><b>Fig. 22.7</b></A>
<P><I>Browse your way to the .mdb file that holds your data.</I></P>
<ol start=6>
<li><P> Click OK to finalize the database selection, and then in the ODBC Microsoft Access 97 Setup dialog box, click OK to finalize the data-source creation process. Finally, click OK in the ODBC Data Source Administrator dialog box.</P>
</ol>
<P>Your system is now set up to access the DeptStore.mdb database file with the Microsoft Access ODBC driver.</P>
<P><A ID="I10" NAME="I10"><B>Creating the Basic Employee Application</B></A></P>
<P>Now that you have your data source created and registered, it's time to create the basic Employee application. The steps that follow lead you through this process. After you've completed these steps, you'll have an application that can access and view
the Employees table of the department store database:</P>
<ol>
<li><P> Select <U>F</U>ile, <U>N</U>ew from Developer Studio's menu bar. Click the Projects tab. </P>
<li><P> Select MFC AppWizard (exe) and type <B>Employee</B> into the <U>N</U>ame box, as shown in Figure 22.8. Click OK. The Step 1 dialog box appears.</P>
</ol>
<A HREF="WfigC08.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC08.gif"><b>Fig. 22.8</b></A>
<P><I>Create an ordinary MFC application with AppWizard.</I></P>
<ol start=3>
<li><P> Select <U>S</U>ingle Document, as shown in Figure 22.9, to ensure that the Employee application will not allow more than one window to be open at a time. Click <U>N</U>ext.</P>
</ol>
<A HREF="WfigC09.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC09.gif"><b>Fig. 22.9</b></A>
<P><I>Create a Single Document application.</I></P>
<ol start=4>
<li><P> Select the <U>D</U>atabase View Without File Support option, as shown in Figure 22.10, so that AppWizard will generate the classes you need to view the contents of a database. This application will not use any supplemental files besides the
database, so it doesn't need file (serializing) support. Click the Data <U>S</U>ource button to connect the application to the data source you set up earlier.</P>
</ol>
<A HREF="WfigC10.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC10.gif"><b>Fig. 22.10</b></A>
<P><I>Arrange for a Database View, but no other file support.</I></P>
<ol start=5>
<li><P> In the Database Options dialog box, drop down the ODBC list and select the Department Store data source, as shown in Figure 22.11. Click OK. </P>
</ol>
<A HREF="WfigC11.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC11.gif"><b>Fig. 22.11</b></A>
<P><I>Choose your Department Store data source.</I></P>
<ol start=6>
<li><P> In the Select Database Tables dialog box, select the Employees table, as shown in Figure 22.12, and click OK. The Step 2 dialog box reappears, filled in as shown in Figure 22.13.</P>
<P> You've now associated the Employees table of the Department Store data source with the Employee application. Click <U>N</U>ext to move to step 3.</P>
</ol>
<A HREF="WfigC12.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC12.gif"><b>Fig. 22.12</b></A>
<P><I>Select which tables from the data source you want to use in this </I><I>application.</I></P>
<A HREF="WfigC13.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC13.gif"><b>Fig. 22.13</b></A>
<P><I>After selecting the data source, the Step 2 dialog box should look like </I><I>this.</I></P>
<ol start=7>
<li><P> Accept the default, No Compound Document Support, and click <U>N</U>ext. </P>
<li><P> In the Step 4 dialog box, turn off the Printing and Print Preview option so that the dialog box resembles Figure 22.14. Click <U>N</U>ext. </P>
</ol>
<A HREF="WfigC14.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC14.gif"><b>Fig. 22.14</b></A>
<P><I>Turn off print support.</I></P>
<ol start=9>
<li><P> Accept the defaults for step 5 by clicking <U>N</U>ext. In step 6, just click <U>F</U>inish to finalize your selections for the Employee application. The New Project Information dialog box that appears should look like Figure 22.15.</P>
</ol>
<A HREF="WfigC15.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC15.gif"><b>Fig. 22.15</b></A>
<P><I>The application summary mentions the data source as well as the usual </I><I>information.</I></P>
<ol start=10>
<li><P> Click OK, and AppWizard creates the basic Employee application.</P>
</ol>
<P>At this point, you can compile the application by clicking the Build button on Developer Studio's toolbar, by selecting the <U>B</U>uild, <U>B</U>uild command from the menu bar, or by pressing F7 on your keyboard. After the program has compiled, select
the <U>B</U>uild, <U>E</U>xecute command from the menu bar or press Ctrl+F5 to run the program. When you do, you see the window shown in Figure 22.16. You can use the database controls in the application's toolbar to navigate from one record in the
Employee table to another. However, nothing appears in the window because you've yet to associate controls with the fields in the table that you want to view. You'll do that in the following section.</P>
<A HREF="WfigC16.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC16.gif"><b>Fig. 22.16</b></A>
<P><I>The basic Employee application looks nice but doesn't do much.</I></P>
<P><A ID="I11" NAME="I11"><B>Creating the Database Display</B></A></P>
<P>The next step in creating the Employee database application is to modify the form that displays data in the application's window. Because this form is just a special type of dialog box, it's easy to modify with Developer Studio's resource editor, as
you'll discover as you complete the following steps:</P>
<ol>
<li><P> Select the Resource View tab to display the application's resources.</P>
<li><P> Open the resource tree by clicking + next to the Employee Resources folder. Then, open the Dialog resource folder the same way. Double-click the <font color="#008000">IDD_EMPLOYEE_FORM</font> dialog box ID to open the dialog box into the resource
editor, as shown in Figure 22.17.</P>
</ol>
<A HREF="WfigC17.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC17.gif"><b>Fig. 22.17</b></A>
<P><I>Open the dialog box in the resource editor.</I></P>
<ol start=3>
<li><P> Click the static string in the center of the dialog box to select it, and then press Delete to remove the string from the dialog box.</P>
<li><P> Use the dialog box editor's tools to create the dialog box shown in Figure 22.18 by adding edit boxes and static labels. (Editing dialog boxes is introduced in <A HREF="index02.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index02.htm" target="text">Chapter 2</A>, "Dialogs and Controls.")
Give the edit boxes the following IDs: <B>IDC_EMPLOYEE_ID</B>, <B>IDC_EMPLOYEE_NAME</B>, <B>IDC_EMPLOYEE_RATE</B>, and <B>IDC_EMPLOYEE_DEPT</B>. Set the Read-Only style (found on the Styles page of the Edit Properties property sheet) of the <font
color="#008000">IDC_EMPLOYEE_ID</font> edit box.</P>
<P> Each of these edit boxes will represent a field of data in the database. The first edit box is read-only because it will hold the database's primary key, which should never be modified. </P>
</ol>
<A HREF="WfigC18.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC18.gif"><b>Fig. 22.18</b></A>
<P><I>Create a dialog box to be used in your database form.</I></P>
<ol start=5>
<li><P> Choose <U>V</U>iew, Class<U>W</U>izard to bring up ClassWizard, and click the Member Variables tab.</P>
<li><P> With the <font color="#008000">IDC_EMPLOYEE_DEPT</font> resource ID selected, click the <U>A</U>dd Variable button. The Add Member Variable dialog box appears.</P>
<li><P> Click the arrow next to the Member Variable <U>N</U>ame drop-down list, and select <font color="#008000">m_pSet->m_DeptID</font>, as shown in Figure 22.19.</P>
</ol>
<A HREF="WfigC19.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC19.gif"><b>Fig. 22.19</b></A>
<P><I>Connect the </I><I><font color="#008000">IDC_EMPLOYEE_DEPT</font></I><I> control with the </I><I><font color="#008000">m_DeptID</font></I><I> member variable </I><I>of the recordset.</I></P>
<ol start=8>
<li><P> Associate other member variables (<font color="#008000">m_pSet->EmployeeID</font>, <font color="#008000">m_pSet</font><font color="#008000">-</font><font color="#008000">>EmployeeName</font>, and <font
color="#008000">m_pSet->EmployeeRate</font>) with the edit controls in the same way. When you're finished, the Member Variables page of the MFC ClassWizard property sheet should look like Figure 22.20.</P>
<P> By selecting member variables of the application's <font color="#008000">CEmployeeSet</font> class (derived from MFC's <font color="#008000">CRecordset</font> class) as member variables for the controls in Database view, you're establishing a
connection through which data can flow between the controls and the data source.</P>
</ol>
<A HREF="WfigC20.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC20.gif"><b>Fig. 22.20</b></A>
<P><I>All four controls are connected to member variables.</I></P>
<ol start=9>
<li><P> Click the OK button in the MFC ClassWizard property sheet in order to finalize your changes.</P>
</ol>
<P>You've now created a data-display form for the Employee application. Build and execute the program again, and you see the window shown in Figure 22.21. Now the application displays the contents of records in the Employee database table. Use the
database controls in the application's toolbar to navigate from one record in the Employee table to another.</P>
<A HREF="WfigC21.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC21.gif"><b>Fig. 22.21</b></A>
<P><I>The Employee application now displays data in its window.</I></P>
<P>After you've examined the database, try updating a record. To do this, simply change one of the record's fields (except the employee ID, which is the table's primary key and can't be edited). When you move to another record, the application
automatically updates the modified record. The commands in the application's <U>R</U>ecord menu also enable you to navigate through the records in the same manner as the toolbar buttons.</P>
<P>Notice that you've created a fairly sophisticated database-access program without writing a single line of C++ code—a pretty amazing feat. Still, the Employee application is limited. For example, it can't add or delete records. As you may have
guessed, that's the next piece of the database puzzle that you'll add.</P>
<P><A ID="I12" NAME="I12"><B>Adding and Deleting Records</B></A></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -