⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch22.htm

📁 Using Visual C++ 6.0 一本关于Visual C++ 6.0基本编程应用的书籍。
💻 HTM
📖 第 1 页 / 共 4 页
字号:

<DL>
	<DT><I></I></DT>
	<DD><B>5. </B>Click the Select button. The Select Database file selector appears.
	Use the selector to locate and select the DeptStore.mdb file (see Figure 22.7).
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc07.gif')"><B>FIG. 22.7</B></A><B> </B><I>Browse
your way to the .mdb file that holds your data.</I></P>
<P>

<DL>
	<DT><I></I></DT>
	<DD><B>6. </B>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 and close the
	Control Panel.
	<P>
</DL>

<P>Your system is now set up to access the DeptStore.mdb database file with the Microsoft
Access ODBC driver.</P>
<P>
<H3><A NAME="Heading8"></A>Creating the Basic Employee Application</H3>
<P>Now that you have created and registered your data source, it's time to create
the basic Employee application. The steps that follow lead you through this process.
After you complete these steps, you will have an application that can access and
view the Employees table of the Department Store database:</P>

<DL>
	<DD><B>1. </B>Select File, New from Developer Studio's menu bar. Click the Projects
	tab.
	<P>
	<DT></DT>
	<DD><B>2. </B>Select MFC AppWizard (exe) and type <B>Employee</B> in the Project
	Name box, as shown in Figure 22.8. Click OK. The Step 1 dialog box appears.
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc08.gif')"><B>FIG. 22.8</B></A><B> </B><I>Create
an ordinary MFC application with AppWizard.</I></P>
<P>

<DL>
	<DT><I></I></DT>
	<DD><B>3. </B>Select Single Document, as shown in Figure 22.9, to ensure that the
	Employee application doesn't allow more than one window to be open at a time. Click
	Next.
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc09.gif')"><B>FIG. 22.9</B></A><B> </B><I>Create
a single- document application.</I></P>
<P>

<DL>
	<DD><B>4. </B>Select the Database View Without File Support option, as shown in Figure
	22.10, so that AppWizard will generate the classes you need in order 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 Source
	button to connect the application to the data source you set up earlier.
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc10.gif')"><B>FIG. 22.10</B></A><B> </B><I>Arrange
for a database view but no other file support.</I></P>
<P>

<DL>
	<DD><B>5. </B>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>
</DL>

<P><A HREF="javascript:popUp('22uvc11.gif')"><B>FIG. 22.11</B></A><B> </B><I>Choose
the Department Store data source.</I></P>
<P>

<DL>
	<DD><B>6. </B>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>
	<DT></DT>
	<DD>You've now associated the Employees table of the Department Store data source
	with the Employee application. Click Next to move to Step 3.
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc12.gif')"><B>FIG. 22.12</B></A><B> </B><I>Select
which tables from the data source you want to use in this application.</I></P>
<P>

<DL>
	<DT><I></I></DT>
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc13.gif')"><B>FIG. 22.13</B></A><B> </B><I>After
selecting the data source, the Step 2 dialog box looks like this.</I></P>
<P>

<DL>
	<DD><B>7. </B>Accept the default (None) no compound document support and click Next.
	<P>
	<DT></DT>
	<DD><B>8. </B>In the Step 4 dialog box, turn off the Printing and Print Preview option
	so that the dialog box resembles Figure 22.14. Click Next.
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc14.gif')"><B>FIG. 22.14</B></A><B> </B><I>Turn
off print support.</I></P>
<P>

<DL>
	<DD><B>9. </B>Accept the defaults for Step 5 by clicking Next. In Step 6, click Finish
	to finalize your selections for the Employee application. Figure 22.15 shows the
	New Project Information dialog box that appears.
	<P>
	<DT></DT>
	<DD><B>10. </B>Click OK, and AppWizard creates the basic Employee application.
	<P>
</DL>

<P>At this point, you can compile the application by clicking the Build button on
Developer Studio's toolbar, by selecting the Build, Build command from the menu bar,
or by pressing F7 on your keyboard. After the program has compiled, select the Build,
Execute 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 will do that in the following
section.</P>
<P><A HREF="javascript:popUp('22uvc15.gif')"><B>FIG. 22.15</B></A><B> </B><I>The
application summary mentions the data source as well as the usual information.</I></P>
<P><A HREF="javascript:popUp('22uvc16.gif')"><B>FIG. 22.16</B></A><B> </B><I>The
basic Employee application looks nice but doesn't do much.</I></P>
<H3><I></I></H3>
<H3><A NAME="Heading9"></A>Creating the Database Display</H3>
<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 will discover while completing the following steps:</P>

<DL>
	<DT></DT>
	<DD><B>1. </B>In the workspace window, select the Resource View tab to display the
	application's resources.
	<P>
	<DT></DT>
	<DD><B>2. </B>Open the resource tree by clicking + next to the Employee resources
	folder. Then, open the Dialog resource folder the same way. Double-click the IDD_EMPLOYEE_FORM
	dialog box ID to open the dialog box into the resource editor, as shown in Figure
	22.17.
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc17.gif')"><B>FIG. 22.17</B></A><B> </B><I>Open
the dialog box in the resource editor.</I></P>
<P>

<DL>
	<DD><B>3. </B>Click the static string in the center of the dialog box to select it,
	and then press the Delete key to remove the string from the dialog box.
	<P>
	<DT></DT>
	<DD><B>4. </B>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 Chapter 2, &quot;Dialogs and Controls.&quot;) 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 IDC_EMPLOYEE_ID edit box.
	<P>
	<DT></DT>
	<DD>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>
</DL>

<P><A HREF="javascript:popUp('22uvc18.gif')"><B>FIG. 22.18</B></A><B> </B><I>Create
a dialog box to be used in your database form.</I></P>
<P>

<DL>
	<DD><B>5. </B>Choose View, ClassWizard to open ClassWizard, and click the Member
	Variables tab.
	<P>
	<DT></DT>
	<DD><B>6. </B>With the IDC_EMPLOYEE_DEPT resource ID selected, click the Add Variable
	button. The Add Member Variable dialog box appears.
	<P>
	<DT></DT>
	<DD><B>7. </B>Click the arrow next to the Member Variable Name drop-down list and
	select m_pSet-&gt;m_DeptID, as shown in Figure 22.19. Leave the type as CString and
	click OK to add the variable.
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc19.gif')"><B>FIG. 22.19</B></A><B> </B><I>Connect
the IDC_EMPLOYEE_DEPT control with the m_DeptID member variable of the recordset.</I></P>
<P>

<DL>
	<DD><B>8. </B>Associate other member variables (m_pSet-&gt;EmployeeID, m_pSet-&gt;EmployeeName,
	and m_pSet-&gt;EmployeeRate) with the edit controls in the same way. When you're
	finished, the Member Variables page of the MFC ClassWizard property sheet will look
	like Figure 22.20.
	<P>
	<DT></DT>
	<DD>By selecting member variables of the application's CEmployeeSet class (derived
	from MFC's CRecordset 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>
</DL>

<P><A HREF="javascript:popUp('22uvc20.gif')"><B>FIG. 22.20</B></A><B> </B><I>All
four controls are connected to member variables.</I></P>

<DL>
	<DD><B>9. </B>Click the OK button in the MFC ClassWizard property sheet to finalize
	your changes.
	<P>
</DL>

<P>You've now created a data display form for the Employee application. Build and
execute the program again, and you will 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>
<P><A HREF="javascript:popUp('22uvc21.gif')"><B>FIG. 22.21</B></A><B> </B><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 Record 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 sophisticated database-access program without writing
a single line of C++ code--an 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, which you will add.</P>
<P>
<H3><A NAME="Heading10"></A>Adding and Deleting Records</H3>
<P>When you can add and delete records from a database table, you will have a full-featured
program for manipulating a flat (that is, not a relational) database. In this case,
the flat database is the Employees table of the Department Store relational database.
Adding and deleting records in a database table is an easier process than you might
believe, thanks to Visual C++'s CRecordView and CRecordSet classes, which provide
all the member functions you need in order to accomplish these common database tasks.
You will need to add some menu items to the application, as first discussed in Chapter
8, &quot;Building a Complete Application: ShowString.&quot; Follow these steps to
include add and delete commands in the Employee application:</P>

<DL>
	<DT></DT>
	<DD><B>1. </B>Select the ResourceView tab, open the Menu folder, and double-click
	the IDR_MAINFRAME menu ID. The menu editor appears, as shown in Figure 22.22.
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc22.gif')"><B>FIG. 22.22</B></A><B> </B><I>Developer
Studio's menu editor is in the pane on the right.</I></P>
<P>

<DL>
	<DD><B>2. </B>Click the Record menu item to open it, and click the blank menu item
	at the bottom of the menu. Choose View, Properties and pin the Menu Item Properties
	dialog box in place.
	<P>
	<DT></DT>
	<DD><B>3. </B>In the ID edit box, enter <B>ID_RECORD_ADD</B> and in the Caption box,
	enter <B>&amp;Add Record</B>, as shown in Figure 22.23. This adds a new command to
	the Record menu.
	<P>
	<DT></DT>
	<DD><B>4. </B>In the next blank menu item, add a delete command with the ID <B>ID_RECORD_DELETE</B>
	and the caption <B>&amp;Delete Record</B>.
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc23.gif')"><B>FIG. 22.23</B></A><B> </B><I>Add
a menu item that adds a record to the Employee table.</I></P>
<P>Next, you will connect these commands to toolbar buttons, as first discussed in
Chapter 9, &quot;Status Bars and Toolbars.&quot; Follow these steps:</P>

<DL>
	<DT></DT>
	<DD><B>1. </B>In the ResourceView pane, open the Toolbar folder and then double-click
	the IDR_MAINFRAME ID. The application's toolbar appears in the resource editor.
	<P>
	<DT></DT>
	<DD><B>2. </B>Click the blank toolbar button to select it, and then use the editor's
	tools to draw a red plus on the button.
	<P>
	<DT></DT>
	<DD><B>3. </B>Double-click the new button in the toolbar. The Toolbar Button Properties
	property sheet appears. Select ID_RECORD_ADD in the ID box to connect this button

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -