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

📄 ch22.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML>

<HEAD>

<TITLE>Special Edition Using Visual C++ 5 - Chapter 22</TITLE>

<LINK REL="Next" HREF="ch23.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch23.htm">

<LINK REL="Previous" HREF="ch21.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch21.htm"></HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000">



<H2><A ID="I1" NAME="I1"><B>Chapter 22</B></A></H2>

<H2><A ID="I2" NAME="I2"></A><B>Database Access</B></H2>

<hr>

<P>Without a doubt, databases are one of the most popular computer applications. Virtually every business uses databases to keep track of everything from their customer list to the company payroll. Unfortunately, there are many different types of database 
applications, each of which defines its own file layouts and rules. In the past, programming database applications was a nightmare, because it was up to the programmer to figure out all the intricacies of accessing the different types of database 
files.</P>

<P>Now, however, Visual C++ includes classes that are built upon the ODBC (Open Database Connectivity) and DAO (Data Access Objects) systems. Believe it or not, by using AppWizard, you can create a simple database program without writing even a single 
line of C++ code. More complex tasks do require some programming, but not as much as you might think. </P>

<P>In this chapter, you get an introduction to programming with Visual C++'s ODBC classes. You'll also learn about the similarities and differences between ODBC and DAO. Along the way, you create a database application that can not only display records in 
a database, but also update, add, delete, sort, and filter records.</P>

<ul>

<li> <B>Basic database concepts</B></P>

<P>  Regardless of what type of database you want to access, the basic concepts, like records and fields,remain the same.</P>

<li> <B>How the flat and relational database models differ</B></P>

<P>  Flat model databases are the simplest type of databases, but relational databases are the most efficient and powerful.</P>

<li> <B>MFC's ODBC database classes</B></P>

<P>  The ODBC classes enable you to access databases without all the hassles previously associated with database programming.</P>

<li> <B>How to use AppWizard to create a basic ODBC application</B></P>

<P>  AppWizard is an amazing tool for getting your database application up and running quickly and easily.</P>

<li> <B>How to implement add, delete, update, sort, and filter </B><B>abiltities to your database program</B></P>

<P>  Although AppWizard can create a functional database program for you, you have to do a little programming to incorporate more sophisticated database commands into your application.</P>

<li> <B>MFC's DAO classes</B></P>

<P>  MFC also features a second set of database classes that are best suited to creating applications that will access .mdb database files. Although the DAO classes are not suited to all database projects, they are more powerful than their ODBC 
counterparts.</P>

<li> <B>MFC's OLE DB</B></P>

<P>  When you need to integrate data from a database with data from another kind of program, OLE DB can make it possible.</P>

</ul>

<H3><A ID="I3" NAME="I3"><B>Understanding Database Concepts</B></A></H3>

<P>Before you can write database applications, you have to know a little about how databases work. Databases have come a long way since their invention, so there's much you can learn about them. This section provides a quick introduction to basic database 
concepts, including the two main types of databases: flat and relational.</P>

<P><A ID="I4" NAME="I4"><B>Using the Flat Database Model</B></A></P>

<P>Simply put, a <I>database</I> is a collection of records. Each record in the database is comprised of fields, and each field contains information that's related to that specific record. For example, suppose you have an address database. In this 
database, you have one record for each person. Each record contains six fields: the person's name, street address, city, state, ZIP code, and phone number. So, a single record in your database might look like this:</P>

<pre><font color="#008000">NAME: Ronald Wilson</font></pre>

<pre><font color="#008000">STREET: 16 Tolland Dr.</font></pre>

<pre><font color="#008000">CITY: Hartford</font></pre>

<pre><font color="#008000">STATE: CT</font></pre>

<pre><font color="#008000">ZIP: 06084</font></pre>

<pre><font color="#008000">PHONE: 860-555-3542</font></pre>

<P>Your entire database will contain many records like this one , with each record containing information about a different person. To find a person's address or phone number, you search for the name. When you find the name, you also find all the 
information that's included in the record with the name. </P>

<P>This type of database system uses the <I>flat database model</I>. For home use or for small businesses, the simple flat database model can be a powerful tool. However, for large databases that must track dozens, or even hundreds, of fields of data, a 
flat database can lead to repetition and wasted space. Suppose you run a large department store and want to track some information about your employees, including their names, departments, manager&#146;s names, and so on. If you have 10 people in Sporting 
Goods, the name of the Sporting Goods manager will be repeated in each of those 10 records. When Sporting Goods gets a new manager, all 10 records will have to be updated. It would be much simpler if each employee record could be <I>related</I> to another 
database of departments and manager names.</P>

<P><B> </B><A ID="I5" NAME="I5"><B>Using the Relational Database Model</B></A></P>

<P>A <I>relational database</I> is like several flat databases linked together. Using a relational database, you cannot only search for individual records as you can with a flat database, but you can also relate one set of records to another. This enables 
you to store data much more efficiently. Each set of records in a relational database is called a <I>table</I>. The links are accomplished through <I>keys</I>, values that define a record. (For example, the employee ID might be the key to an employee 
table.) </P>

<P>The example relational database that you use in this chapter was created using Microsoft Access. The database is a simple system for tracking employees, managers, and the departments for which they work. Figures 22.1, 22.2, and 22.3 show the tables: 
the Employees table contains information about each of the store's employees, the Managers table contains information about each store department's manager, and the Departments table contains information about the departments themselves. (This database is 
very simple and probably not usable in the real world.)</P>

<A HREF="WfigC01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC01.gif"><b>Fig. 22.1</b></A>

<P><I>The Employees table contains data fields for each store employee.</I></P>

<A HREF="WfigC02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC02.gif"><b>Fig. 22.2</b></A>

<P><I>The Managers table contains information about each store department's </I><I>manager.</I></P>

<A HREF="WfigC03.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC03.gif"><b>Fig. 22.3</b></A>

<P><I>The Departments table contains data about each store department.</I></P>

<P><A ID="I6" NAME="I6"><B>Accessing a Database</B></A></P>

<P>Relational databases are accessed using some sort of database scripting language. The most commonly used database language is <I>SQL</I>, which is used not only to manage databases on desktop computers but also on the huge databases used by banks, 
schools, corporations, and other institutions with sophisticated database needs. By using a language like SQL, you can compare information in the various tables of a relational database and extract results that are made up of data fields from one or more 
tables combined.</P>

<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">

<P>Most developers pronounce SQL as &quot;Sequel.&quot;</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<P>Learning SQL, though, is a large task, one that is way beyond the scope of this book (let alone this chapter). In fact, entire college-level courses are taught on the design, implementation, and manipulation of databases. Because there isn't space in 
this chapter to cover relational databases in any useful way, you'll use the Employee table (refer to Figure 22.1) of the Department Store database in the sample database program you'll soon develop. When you're finished creating the application, you'll 
have learned one way you can update the tables of a relational database without learning even a word of SQL. (Those of you who live and breathe SQL will enjoy <A HREF="index23.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index23.htm" target="text">Chapter 23</A>, &#147;SQL and the Enterprise 
Edition.&#148;)</P>

<P><B> </B><A ID="I7" NAME="I7"><B>The Visual C++ ODBC Classes</B></A></P>

<P>When you create a database program with Visual C++'s AppWizard, you end up with an application that draws extensively upon the various ODBC classes that have been incorporated into MFC. The most important of these classes are <font 
color="#008000">CDatabase</font>, <font color="#008000">CRecordset</font>, and <font color="#008000">CRecordView</font>.</P>

<P>AppWizard automatically generates the code needed to create an object of the <font color="#008000">CDatabase</font> class. This object represents the connection between your application and the data source that you'll be accessing. In most cases, using 
the <font color="#008000">CDatabase</font> class in an AppWizard-generated program is transparent to you, the programmer. All the details are handled by the framework.</P>

<P>AppWizard also generates the code needed to create a <font color="#008000">CRecordset</font> object for the application. The <font color="#008000">CRecordset</font> object represents the actual data that's currently selected from the data source, and 
its member functions manipulate the data from the database.</P>

<P>Finally, the <font color="#008000">CRecordView</font> object in your database program takes the place of the normal view window you're used to using in AppWizard-generated applications. A <font color="#008000">CRecordView</font> window is like a dialog 
box that's being used as the application's display. This dialog box-type of window retains a connection to the application's <font color="#008000">CRecordset</font> object, hustling data back and forth between the program, the window's controls, and the 
recordset. When you first create a new database application with AppWizard, it's up to you to add edit controls to the <font color="#008000">CRecordView</font> window. These edit controls must be bound to the database fields they represent, so the 
application framework knows where to display the data you want to view. </P>

<P>In the next section, you'll see how these various database classes fit together as you build the Employee application step-by-step.</P>

<H3><A ID="I8" NAME="I8"><B>Creating an ODBC Database Program</B></A></H3>

<P>Although creating a simple ODBC database program is easy with Visual C++, there are number of steps you must complete:</P>

<ol>

<li><P> Register the database with the system.</P>

<li><P> Use AppWizard to create the basic database application.</P>

<li><P> Add code to the basic application in order to implement features not automatically supported by AppWizard.</P>

</ol>

<P>In the following sections, you'll see how to perform these steps as you create the Employee application, which enables you to add, delete, update, sort, and view records in the Employees table of the sample department store database.</P>

<P><A ID="I9" NAME="I9"><B>Registering the Database</B></A></P>

<P>Before you can create a database application, you must register the database that you want to access as a data source that you can access through the ODBC driver. Follow these steps to accomplish this important task:</P>

<ol> 

<li><P> Create a folder called <B>Database</B> on your hard disk, and copy the file named DeptStore.mdb from this book's CD-ROM to the new Database folder.</P>

<P>  The DeptStore.mdb file is a database created with Microsoft Access. You'll be using this database as the data source for the Employee application.</P>

<li><P> From the Windows 95 Start menu, run Control Panel. When Control Panel appears, double-click the 32-Bit ODBC icon. The Data Sources dialog box appears, as shown in Figure 22.4.</P>

</ol>

<A HREF="WfigC04.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC04.gif"><b>Fig. 22.4</b></A>

<P><I>Connecting a data source to your application starts with the ODBC Data </I><I>Source Administrator.</I></P>

<ol start=3>

<li><P> Click the Add button. The Create New Data Source dialog box appears. Select the Microsoft Access Driver from the list of drivers as shown in Figure 22.5 and click Finish.</P>

<P>  The Microsoft Access Driver is now the ODBC driver that will be associated with the data source you'll be creating for the Employee application.</P>

</ol>

<A HREF="WfigC05.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC05.gif"><b>Fig. 22.5</b></A>

<P><I>Creating a new data source is as simple as choosing Access from a list </I><I>of drivers.</I></P>

<ol start=4>

<li><P> When the ODBC Microsoft Access 7.0 Setup dialog box appears, enter <B>Department Store</B> into the Data Source <U>N</U>ame text box, and enter <B>Department Store Sample</B> in the <U>D</U>escription text box, as shown in Figure 22.6.</P>

<P>  The datasource name is simply a way of identifying the specific data source that you're creating. The Description field enables you to include more specific information about the data source.</P>

</ol>

⌨️ 快捷键说明

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