?? ch22.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
var fullURL = document.location;
var textURL = fullURL.toString();
var URLlen = textURL.length;
var lenMinusPage = textURL.lastIndexOf("/");
lenMinusPage += 1;
var fullPath = textURL.substring(0,lenMinusPage);
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '<link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></head>';
zhtm += '<BODY bgcolor="#FFFFFF">';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '</BODY></HTML>';
window.popUpWin.document.write(zhtm);
window.popUpWin.document.close();
// Johnny Jackson 4/28/98
}
//-->
</SCRIPT>
<link rel="stylesheet" href="/includes/stylesheets/ebooks.css">
<TITLE>Special Edition Using Visual C++ 6 -- Ch 22 -- Database Access</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
Special Edition Using Visual C++ 6</H1>
</CENTER>
<CENTER>
<P><A HREF="../ch21/ch21.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch23/ch23.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A>
<HR>
</CENTER>
<CENTER>
<H1>- 22 -</H1>
</CENTER>
<CENTER>
<H1>Database Access</H1>
</CENTER>
<UL>
<LI><A HREF="#Heading1">Understanding Database Concepts</A>
<UL>
<LI><A HREF="#Heading2">Using the Flat Database Model</A>
<LI><A HREF="#Heading3">Using the Relational Database Model</A>
<LI><A HREF="#Heading4">Accessing a Database</A>
<LI><A HREF="#Heading5">The Visual C++ ODBC Classes</A>
</UL>
<LI><A HREF="#Heading6">Creating an ODBC Database Program</A>
<UL>
<LI><A HREF="#Heading7">Registering the Database</A>
<LI><A HREF="#Heading8">Creating the Basic Employee Application</A>
<LI><A HREF="#Heading9">Creating the Database Display</A>
<LI><A HREF="#Heading10">Adding and Deleting Records</A>
<LI><A HREF="#Heading11">Examining the OnRecordAdd() Function</A>
<LI><A HREF="#Heading12">Examining the OnMove() Function</A>
<LI><A HREF="#Heading13">Examining the OnRecordDelete() Function</A>
<LI><A HREF="#Heading14">Sorting and Filtering</A>
<LI><A HREF="#Heading15">Examining the OnSortDept() Function</A>
<LI><A HREF="#Heading16">Examining the DoFilter() Function</A>
</UL>
<LI><A HREF="#Heading17">Choosing Between ODBC and DAO</A>
<LI><A HREF="#Heading18">OLE DB</A>
</UL>
<P>
<HR SIZE="4">
<CENTER>
<H1></H1>
</CENTER>
<P>Without a doubt, databases are one of the most popular computer applications.
Virtually every business uses databases to keep track of everything from its 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.
As a Visual C++ developer, you have a somewhat simpler task because MFC includes
classes built on the ODBC (Open Database Connectivity) and DAO (Data Access Objects)
systems. Other Microsoft database technologies are gaining MFC support as well.</P>
<P>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>This chapter gives you an introduction to programming with Visual C++'s ODBC classes.
You will also learn about the similarities and differences between ODBC and DAO.
Along the way, you will create a database application that can not only display records
in a database but also update, add, delete, sort, and filter records.</P>
<P>
<H2><A NAME="Heading1"></A>Understanding Database Concepts</H2>
<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>
<H3><A NAME="Heading2"></A>Using the Flat Database Model</H3>
<P>Simply put, a <I>database</I> is a collection of records. Each record in the database
is composed of fields, and each field contains information 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. A single record in your
database might look like this:</P>
<P>
<PRE>NAME: Ronald Wilson
STREET: 16 Tolland Dr.
CITY: Hartford
STATE: CT
ZIP: 06084
PHONE: 860-555-3542
</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 name, department, manager's name, and so on. If you have 10 people in Sporting
Goods, the name of the Sporting Goods manager is repeated in each of those 10 records.
When Sporting Goods hires a new manager, all 10 records 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>
<H3><A NAME="Heading3"></A>Using the Relational Database Model</H3>
<P>A <I>relational database</I> is like several flat databases linked together. Using
a relational database, you can not only search for individual records, as you can
with a flat database but 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 sample 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 store employee, 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>
<P><A HREF="javascript:popUp('22uvc01.gif')"><B>FIG. 22.1</B></A><B> </B><I>The Employees
table contains data fields for each store employee.</I></P>
<P><A HREF="javascript:popUp('22uvc02.gif')"><B>FIG. 22.2</B></A><B> </B><I>The Managers
table contains information about each store department's manager.</I></P>
<P><A HREF="javascript:popUp('22uvc03.gif')"><B>FIG. 22.3</B></A><B> </B><I>The Departments
table contains data about each store department.</I></P>
<H3><I></I></H3>
<H3><A NAME="Heading4"></A>Accessing a Database</H3>
<P>Relational databases are accessed by using some sort of database scripting language.
The most commonly used database language is the Structured Query Language (SQL),
which is used to manage not only databases on desktop computers but also huge databases
used by banks, schools, corporations, and other institutions with sophisticated database
needs. By using a language such as SQL, you can compare information in the various
tables of a relational database and extract results made up of data fields from one
or more tables combined.</P>
<BLOCKQUOTE>
<P>
<HR>
<strong>TIP:</strong> Most developers pronounce SQL as <I>Sequel</I>.
<HR>
</BLOCKQUOTE>
<P>Learning SQL, though, is a large task, one that is 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 will use the
Employee table (refer to Figure 22.1) of the Department Store database in the sample
database program you will soon develop. When you finish creating the application,
you will have learned one way to update the tables of a relational database without
knowing even a word of SQL. (Those of you who live and breathe SQL will enjoy Chapter
23, "SQL and the Enterprise Edition.")</P>
<P>
<H3><A NAME="Heading5"></A>The Visual C++ ODBC Classes</H3>
<P>When you create a database program with Visual C++'s AppWizard, you end up with
an application that draws extensively on the various ODBC classes that have been
incorporated into MFC. The most important of these classes are CDatabase, CRecordset,
and CRecordView.</P>
<P>AppWizard automatically generates the code needed to create an object of the CDatabase
class. This object represents the connection between your application and the data
source that you will be accessing. In most cases, using the CDatabase 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 CRecordset object for the
application. The CRecordset object represents the actual data currently selected
from the data source, and its member functions manipulate the data from the database.</P>
<P>Finally, the CRecordView object in your database program takes the place of the
normal view window you're accustomed to using in AppWizard-generated applications.
A CRecordView 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
CRecordset 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 CRecordView window. These edit
controls must be bound to the database fields they represent so that the application
framework knows where to display the data you want to view.</P>
<P>In the next section, you will see how these various database classes fit together
as you build the Employee application step by step.</P>
<P>
<H2><A NAME="Heading6"></A>Creating an ODBC Database Program</H2>
<P>Although creating a simple ODBC database program is easy with Visual C++, there
are a number of steps you must complete:</P>
<DL>
<DT></DT>
<DD><B>1. </B>Register the database with the system.
<P>
<DT></DT>
<DD><B>2. </B>Use AppWizard to create the basic database application.
<P>
<DT></DT>
<DD><B>3. </B>Add code to the basic application to implement features not automatically
supported by AppWizard.
<P>
</DL>
<P>In the following sections, you will 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>
<H3><A NAME="Heading7"></A>Registering the Database</H3>
<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>
<DL>
<DT></DT>
<DD><B>1. </B>Create a folder called <B>Database</B> on your hard disk and copy the
file named DeptStore.mdb from this book's Web site to the new Database folder. If
you don't have Web access, you can type the three tables into Microsoft Access. If
you don't have Access, you can use a different database program, but you will have
to connect to the data source for that program.
<P>
<DT></DT>
<DD>The DeptStore.mdb file is a database created with Microsoft Access. You will
use this database as the data source for the Employee application.
<P>
<DT></DT>
<DD><B>2. </B>From the Windows Start menu, click Settings and then Control Panel.
When the Control Panel dialog appears, double-click the 32-Bit ODBC icon. The ODBC
Data Source Administrator dialog box appears, as shown in Figure 22.4.
<P>
</DL>
<P><A HREF="javascript:popUp('22uvc04.gif')"><B>FIG. 22.4</B></A><B> </B><I>Connecting
a data source to your application starts with the ODBC Data Source Administrator.</I></P>
<P>
<DL>
<DT><I></I></DT>
<DD><B>3. </B>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>
<DT></DT>
<DD>The Microsoft Access Driver is now the ODBC driver that will be associated with
the data source you create for the Employee application.
<P>
</DL>
<P><A HREF="javascript:popUp('22uvc05.gif')"><B>FIG. 22.5</B></A><B> </B><I>Creating
a new data source is as simple as choosing Access from a list of drivers.</I></P>
<P>
<DL>
<DT><I></I></DT>
<DD><B>4. </B>When the ODBC Microsoft Access 97 Setup dialog box appears, enter <B>Department
Store</B> in the Data Source Name text box and <B>Department Store Sample</B> in
the Description text box, as shown in Figure 22.6.
<P>
<DT></DT>
<DD>The Data Source Name is a way of identifying the specific data source you're
creating. The Description field enables you to include more specific information
about the data source.
<P>
</DL>
<P><A HREF="javascript:popUp('22uvc06.gif')"><B>FIG. 22.6</B></A><B> </B><I>Name
your data source whatever you like.</I></P>
<P>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -