📄 ch09.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<META NAME="Author" Content="Steph Mineart">
<TITLE>Ch 9 -- Using TTable and TDataSet</TITLE>
</HEAD>
<BODY
BACKGROUND="bg1.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/bg1.gif" BGCOLOR="#FFFFFF">
<P ALIGN="CENTER"><IMG SRC="sams.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/sams.gif" WIDTH="75" HEIGHT="24" ALIGN="BOTTOM"
BORDER="0"><BR>
<BR>
<A HREF="index-3.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/index.htm"><IMG SRC="toc.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/toc.gif" WIDTH="40" HEIGHT="40" ALIGN="BOTTOM"
ALT="TOC" BORDER="0" NAME="toc4"></A><A HREF="ch08.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/ch08.htm"><IMG SRC="back-1.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/back.gif"
WIDTH="40" HEIGHT="40" ALIGN="BOTTOM" ALT="BACK" BORDER="0" NAME="toc1"></A><A HREF="ch10.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/ch10.htm"><IMG
SRC="forward.gif" tppabs="http://pbs.mcp.com/ebooks/0672310228/buttonart/forward.gif" WIDTH="40" HEIGHT="40" ALIGN="BOTTOM"
ALT="FORWARD" BORDER="0"
NAME="toc2"></A></P>
<H2 ALIGN="CENTER"><FONT COLOR="#000077">Charlie Calvert's C++ Builder Unleashed</FONT></H2>
<P>
<H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">- 9 -</FONT></H2>
<H2 ALIGN="CENTER"><A
NAME="Heading2"></A><FONT COLOR="#000077">Using TTable and
TDataSet</FONT></H2>
<P>In this chapter, you learn some of the basics about accessing database tables
using the <TT>TTable</TT> object. In particular, the chapter covers the fundamental
information you need to access tables without using SQL. An examination of using
SQL to access tables begins in the next chapter.</P>
<P>In the examples given here, you will be explicitly working with local Paradox
tables, but nearly everything
explained in this chapter applies equally to dBASE
files or to files located on a SQL server such as InterBase or Oracle. I decided
to run these examples against Paradox tables because I wanted to keep this chapter
as simple as possible. Large
portions of this book work exclusively with SQL databases,
but this chapter sticks to the world of local tables.</P>
<P>Looking a little more deeply at the content of this chapter, you can expect to
find information on the following:
<UL>
<LI>The
<TT>TTable</TT> object, which provides the fastest and simplest access to
tables.
<P>
<LI>The <TT>TQuery</TT> object, which is the gateway to the flexible and powerful
world of SQL.
<P>
<LI>The <TT>TDataSet</TT> object, an ancestor of
<TT>TTable</TT> and <TT>TQuery</TT>,
which provides the core functionality for accessing tables and the records that lie
within them.
<P>
<LI>The <TT>TField</TT> object, which gives you access to the fields in a table or
dataset. This object has
powerful descendants such as <TT>TStringField</TT> and <TT>TIntegerField</TT>,
all of which can be created automatically by a visual tool called the Fields Editor.
<P>
<LI>The <TT>TDataSource</TT> object, which serves as an intermediary between
data-aware
controls and the <TT>TTable</TT> and <TT>TQuery</TT> objects.
<P>
<LI>The <TT>TDBGrid</TT> object, which provides a simple and easy way to display
the contents of tables to the user. The <TT>TDBGrid</TT> object supports editing,
deletion, and insertion. It also has support for drop-down lists in lookup fields,
and the capability to assign colors to a column, row, or individual field. I visit
this control more in Chapter 11, "Working with Field Objects."
<P>
<LI>The <TT>TDBEdit</TT> component, which enables you to display a single field from
a single record and to edit or insert the data for that field.
<P>
<LI>Filtering on nonkeyed fields.
<P>
<LI>The <TT>TDatabase</TT> component, which can be handy
when you need to optimize
the way you connect to a server.
<P>
<LI>Connecting to a database without using any visual tools. BCB supports something
called two-way tools, which enable you to do things either visually or in code. Most
of this
chapter takes the visual route, but you can work entirely in code if you
want or if you have some special need that requires that approach. This chapter ends
with a lengthy explanation of how to take this rather unusual approach to BCB database
programming.
<P>
<LI>The order in which database events occur. This is an important subject, which
has to be mastered by serious database programmers.
</UL>
<P>Here is a second way to categorize some of the objects discussed in this chapter:
<UL>
<LI>Nonvisual: <TT>TTable</TT>, <TT>TQuery</TT>, <TT>TDataSet</TT>, <TT>TField</TT>,
and <TT>TDatabase</TT>
<P>
<LI>Visual: <TT>TDBGrid</TT> and <TT>TDBEdit</TT>
<P>
<LI>Link (also nonvisual):<TT>TDataSource</TT>
</UL>
<P>This latter view of the
major database components breaks them down into two major
categories. The nonvisual components enable you to open, close, edit, and otherwise
manipulate tables, records, and fields. The visual components display the tables
to the user so he or she can
view or edit them. The powerful <TT>TDataSource</TT>
object forms a link between the visual and nonvisual database controls. You might
want to think of the nonvisual controls as being the intelligent heart of the BCB
database tools, while the visual
controls are the less intelligent outward show.
The nonvisual controls manipulate the data; the visual controls display it.</P>
<P>The overriding purpose of this chapter is to give you a good overview of the basic
facts about using a BCB database
class called <TT>TDataSet</TT>. <TT>TDataSet</TT>
is the driving force behind both the <TT>TTable</TT> and <TT>TQuery</TT> objects.
It is the root class from which they are both descended. A third component, called
<TT>TStoredProc</TT>, is also
descended from <TT>TDataSet</TT>. It will be discussed
in more depth in Chapters 15, "Working with the Local InterBase Server,"
and 16, "Advanced InterBase Concepts," both of which deal with InterBase.</P>
<P>Specific information
about other database issues will be presented in subsequent
chapters. For instance, the <TT>TQuery</TT> object will be treated in depth in the
next chapter, and more detailed explanations of <TT>TDBGrid</TT>, <TT>TField</TT>,
<TT>TStringField</TT>,
and <TT>TIntegerField</TT> are found in Chapter 11.
<H3><A NAME="Heading3"></A><FONT COLOR="#000077">Understanding the TDataSet Class</FONT></H3>
<P>In the last chapter you were introduced to the Database Explorer, the SQL Monitor,
and
<TT>TDataModule</TT>. It is now time to start digging into some of the technical
details of the <TT>TTable</TT> object. Learning something about these details will
go a long way toward helping you understand the structure of the database tools
supplied
by BCB.</P>
<P><TT>TTable</TT> and <TT>TQuery</TT> inherit most of their functionality from <TT>TDataSet</TT>.
As a result, the <TT>TDataSet</TT> class is one of the most important database objects.
To get started working with it, you need to
concentrate on the hierarchy shown in
Figure 9.1.</P>
<P><TT>TDataSet</TT> contains the abstractions needed to directly manipulate a table.
<TT>TDBDataSet</TT> knows how to handle passwords and other tasks directly associated
with linking to a
specific table. <TT>TTable</TT> knows how to handle indices and
the specific chores associated with linking two tables in a one-to-many relationship.</P>
<P>As you will see in the next chapter, <TT>TQuery</TT> has a deeply rooted and complete
knowledge of how to process SQL statements. The <TT>TStoredProc</TT> object is on
the same level of the hierarchy as <TT>TTable</TT> and <TT>TQuery</TT>. It is used
to process the stored procedures in a SQL database.<BR>
<BR>
<A NAME="Heading4"></A><A
HREF="09ebu01.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/09/09ebu01.jpg">FIGURE 9.1.</A><FONT COLOR="#000077">
</FONT><I>The core hierarchy for <TT>TTable</TT> and <TT>TQuery</TT>.</I></P>
<P>The methods of the <TT>TDataSet</TT> object enable you to open and navigate a
table. Of course, you will
never directly instantiate an object of type <TT>TDataSet</TT>.
Instead, you will usually be working with <TT>TTable</TT>, <TT>TQuery</TT>, or some
other descendant of <TT>TDataSet</TT>. The exact way this system works, and the precise
significance of
<TT>TDataSet</TT>, will become clear as you read through this chapter.</P>
<P>On the most fundamental level, a dataset is nothing more than a set of records,
each containing x number of fields and a pointer to the current record.</P>
<P>On many
occasions, a dataset has a direct, one-to-one correspondence with a physical
table that exists on disk. However, at other times, you may perform a query or other
action that returns a dataset that contains either a subset of one table or a join
between multiple tables. The text that follows, however, sometimes uses the terms
dataset and table interchangeably if it helps to simplify the explanation of a particular
concept.</P>
<P>You will normally instantiate an object of type <TT>TTable</TT>
or <TT>TQuery</TT>
in order to access the functionality of <TT>TDataSet</TT>. Because of this relationship,
the code in the next few sections will always assume the existence of an instance
of class <TT>TTable</TT>. Remember, however, that the
functions under discussion
are part of <TT>TDataSet</TT>, unless the text specifically states otherwise. In
other words, much of what I say here applies to both <TT>TQuery</TT> and <TT>TTable</TT>,
since both of these objects descend from
<TT>TDataSet</TT>.</P>
<P>It's now time for you to begin a direct exploration of <TT>TDataSet</TT>. As you
become familiar with its capabilities, you will begin to understand exactly how BCB
accesses the raw data saved to disk as a database. The key
point to remember is that
nearly every time a BCB programmer opens a table, he or she will be using a class
such as <TT>TTable</TT> or <TT>TQuery</TT>, both of which are merely thin wrappers
around <TT>TDataSet</TT>.
<H3><A NAME="Heading5"></A><FONT
COLOR="#000077">Opening and Closing Datasets</FONT></H3>
<P>The simplest thing you can do with a <TT>TDataSet</TT> is open or close it. This
is therefore an appropriate starting point for an exploration of datasets. In the
sections that follow, you
will drill down deeper and learn more about the thorough
access to databases provided by BCB.</P>
<P>If you are writing code rather than working through the Object Inspector, there
are two different ways to open or close a dataset. You can write the
following line
of code:</P>
<PRE><FONT COLOR="#0066FF">Table1->Open();
</FONT></PRE>
<P>Or, if you prefer, you can set the <TT>Active</TT> property equal to <TT>True</TT>:</P>
<PRE><FONT COLOR="#0066FF">Table1->Active = True;
</FONT></PRE>
<P>There is no difference between the effect produced by these two statements. The
RTL call to <TT>Open</TT>, however, ends up setting <TT>Active</TT> to <TT>True</TT>,
so it may be ever so slightly more efficient to use the <TT>Active</TT> property
directly.</P>
<P>Just as there are two ways to open a table, there are also two ways to close a
table. The simplest way is to call <TT>Close</TT>:</P>
<PRE><FONT COLOR="#0066FF">Table1->Close();
</FONT></PRE>
<P>Or, if you want, you can write the
following:</P>
<PRE><FONT COLOR="#0066FF">Table1->Active = False;
</FONT></PRE>
<P>Once again, there is no substantial difference between these two calls. You should
note, however, that <TT>Close</TT> and <TT>Open</TT> are functions, while
<TT>Active</TT>
is a property.</P>
<P>In this section, you have learned about two methods:</P>
<PRE><FONT COLOR="#0066FF">void __fastcall Open(void);
void __fastcall Close(void);
</FONT></PRE>
<P>You also learned about one property:</P>
<PRE><FONT
COLOR="#0066FF">__property System::Boolean Active;
</FONT></PRE>
<P>It is definitely worthwhile opening up <TT>DB.HPP</TT>, finding the class declaration
for <TT>TDataSet</TT>, and examining the methods shown here as well as some of the
other ones
included in this large object. Remember, most of the rest of this chapter
is dedicated to an examination of <TT>TDataSet</TT>.
<H3><A NAME="Heading6"></A><FONT COLOR="#000077">Navigational Routines</FONT></H3>
<P>After opening a dataset, the next step
is to learn how to move about inside it.
The following rich set of methods and properties from <TT>TDataSet</TT> provides
all the tools you need to access any particular record inside a dataset:</P>
<PRE><FONT COLOR="#0066FF">void __fastcall
First(void);
void __fastcall Last(void);
void __fastcall Next(void);
void __fastcall Prior(void);
property System::Boolean Bof;
property System::Boolean Eof;
System::Integer_fastcall MoveBy(System::Integer Distance);
</FONT></PRE>
<P>Experienced programmers will find these functions very easy to use. Here is a
quick overview of their functionality:
<UL>
<LI>Calling <TT>Table1->First()</TT> moves you to the first record in a table.
<P>
<LI><TT>Table1->Last()</TT> moves
you to the last record.
<P>
<LI><TT>Table1->Next()</TT> moves you one record forward, unless you are at the
end of a table.
<P>
<LI><TT>Table1->Prior()</TT> moves you one record back, unless you are at the
beginning of the table.
<P>
<LI>You can check the <TT>Bof</TT> or <TT>Eof</TT> properties in order to see if
you are at the beginning or the end of a table.
<P>
<LI>The <TT>MoveBy()</TT> function moves you x number of records forward or backward
in a table. There is no
functional difference between calling <TT>Table->Next</TT>
and calling <TT>Table->MoveBy(1)</TT>. Furthermore, calling <TT>Table->Prior</TT>
has the same effect as calling <TT>Table->MoveBy(-1)</TT>. In fact, <TT>Next</TT>
and
<TT>Prior</TT> are one-line functions that call <TT>MoveBy</TT>, exactly as shown
here.
</UL>
<P>Most of these properties and methods are demonstrated in the sample program found
on the CD-ROM accompanying this book as <TT>Navy.dpr</TT>. You can
open this example
directly, or construct it piece by piece by following the description that follows.</P>
<P>To get started using these navigational routines, you should perform the following
steps:
<DL>
<DD><B>1. </B>Place a <TT>TTable</TT>,
<TT>TDataSource</TT>, and <TT>TDBGrid</TT>
on a form.<BR>
<BR>
<B>2.</B> Hook the grid to the data source and the data source to the table.<BR>
<B><BR>
3.</B> Set the <TT>DatabaseName</TT> property of the table to the <TT>DBDEMOS</TT>
alias, or
type in the path to the <TT>demos</TT> subdirectory (<TT>..\BCB 2.0\demos\data</TT>).<BR>
<BR>
<B>4.</B> Set the <TT>TableName</TT> property to the <TT>CUSTOMER</TT> table.
</DL>
<P>If you are having trouble completing these steps, refer to
<TT>Navy.dpr</TT>.</P>
<P>If you run a program that contains a <TT>TDBGrid</TT> control, you will find that
you can iterate through the records in a dataset by manipulating the scrollbars on
the edges of the grid. You can gain the same functionality
by using the <TT>TDBNavigator</TT>
component. However, there are times when you want to move through a table programmatically,
without the use of the built-in visual tools. The next few paragraphs explain how
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -