📄 ch08.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 8 -- Database Basics and Database Tools</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="ch07.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/ch07.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="ch09.htm" tppabs="http://pbs.mcp.com/ebooks/0672310228/ch09.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">- 8 -</FONT></H2>
<H2 ALIGN="CENTER"><A
NAME="Heading2"></A><FONT COLOR="#000077">Database Basics and
Database Tools</FONT></H2>
<P>This chapter provides an introduction to BCB database programming. I start by
showing how to create aliases and simple database applications, and then move on
to a discussion of various conventions that I use when programming databases. Next,
I discuss key database tools that ship with BCB, and close with a discussion of getting
ODBC and TCP/IP set up on your system.</P>
<P>In subsequent chapters I move on
to a discussion of advanced client/server issues.
I am covering such basic material as you find in this chapter in a book on intermediate-to-advanced
programming because there are so many C++ programmers who are new to the database
world. If you have
much experience in C++ desktop and systems programming, but little
database experience, you should take the time to work through this chapter. BCB makes
this subject easy to master, and once you have database tools at your fingertips,
you will be
surprised how many uses you can find for them. This is especially true
in the data-centric, Internet-based world most programmers live in these days.</P>
<P>More advanced database programmers should also read this chapter, because I include
an
overview of the BCB database architecture. All serious developers need to be concerned
with matters covered in this chapter such as Object Repositories, business rules,
the proper use of <TT>TDataModule</TT>, and so on.</P>
<P>More specifically, the
subjects covered in this chapter include introductions
to the following:
<UL>
<LI>The <TT>TTable</TT>, <TT>TQuery</TT>, and <TT>TDataSource</TT> objects. The discussion
of these subjects will show you how to connect to tables with and without SQL.
<P>
<LI>The tools available for creating aliases.
<P>
<LI>The Object Repository, Database Explorer, and SQL Monitor.
<P>
<LI>The <TT>TDataModule</TT> keeps your form clear of database objects, and it consolidates
your database code and rules in
a single object that can be shared in multiple applications.
In particular, it enables you to place nonvisual components, and particularly nonvisual
database components, on a special window that remains hidden at runtime. This window
can be stored
in the Object Repository and can be used to specify business rules
that can be reused by a wide range of projects and programmers.
<P>
<LI>The last part of this chapter covers the different means for accessing data from
inside BCB. For instance,
you can use the BDE, ODBC, and SQL Links. In this section
you learn about the difference between Paradox, dBASE, InterBase, Oracle, and Sybase,
as well as other related matters.
<P>
<LI>The chapter ends with a discussion of using ODBC, and of
setting up TCP/IP on
your system.
</UL>
<P>After you have covered the basics in this chapter, the next step is to move onto
the in-depth discussions of these topics found in the next few chapters. In particular,
the next chapter covers the
<TT>TTable</TT> object in depth, and the following chapter
covers the SQL-based <TT>TQuery</TT> object.</P>
<P>Here is an overview of what lies ahead:
<UL>
<LI>Chapter 8: This chapter, which covers database basics, including introductions
to
<TT>TTable</TT> and <TT>TQuery</TT>
<P>
<LI>Chapter 9: An in-depth look at <TT>TTable</TT> and <TT>TDataSet</TT>
<P>
<LI>Chapter 10: An in-depth look at<TT>TQuery</TT> and an introduction to SQL programming
</UL>
<P>Subsequent chapters in this
section of the book cover intermediate and advanced
database topics. For instance, you will find more material on the database tools
in general and on CASE tools in particular, in Chapters 17 and 18.</P>
<P>The logic behind arranging things this way
is to enable you to first get an introduction
to BCB and databases, and to then dig into the subject once you understand the issues
involved. This way I can keep the simplest subject matter out of the latter chapters
so they can focus on relatively
serious programming issues.</P>
<P>If you are interested primarily in design issues, you can safely skim through
this chapter, looking only for the parts that interest you. If you have not built
a BCB database program before, you should read this
entire chapter through from beginning
to end.
<H3><A NAME="Heading3"></A><FONT COLOR="#000077">On Setting Up the Database Tools</FONT></H3>
<P>BCB comes ready to run Paradox, dBASE, and local InterBase tables. If you have
the client/server version of
the product, it comes with the full InterBase server.
The client/server product also lets you access any DB2, Informix, MS SQL Server,
Sybase, and Oracle you have available. The very first version of BCB does not ship
with built-in support for Access
or FoxPro, but if you have Delphi 3.0 installed
on your system, you can get access to these features automatically.
<DL>
<DT></DT>
</DL>
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>The issue here is that BCB 1.0 ships
with an older version of the BDE than Delphi 3.0, because the new version of the
BDE was not available at BCB ship time. If you can get the most recent version of
the BDE, BCB will work with it automatically, which means you can get at Access
tables.
You can sometimes download the BDE directly from the Borland Web site at <A HREF="javascript:if(confirm('http://www.borland.com/ \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.borland.com/'" tppabs="http://www.borland.com/"><TT>www.borland.com</TT></A>.
If you can't get the most recent version of the BDE, you can use ODBC to, as they
say, access
"Access." <BR>
<BR>
It does not matter that BCB shipped with an earlier version of the BDE than the one
you may be running on your system. The BDE was designed to handle this type of situation,
and it is entirely regular. I use the most
recent version of the BDE with my copy
of BCB, and I never have any hint of trouble.
<HR>
</BLOCKQUOTE>
<P>The primary way to access data from BCB involves the BDE, or Borland Database
Engine. This engine, which is explained in more depth near
the end of this chapter,
is the backbone of the BCB database tools.</P>
<P>Paradox, dBASE, and Access tables can be reached directly from the BDE. To access
the other databases, you also need SQL Links, which is a product that ships with
the
client/server version of Delphi.</P>
<P>One simple way to find out what database access you have from your system involves
dropping down a <TT>TDatabase</TT> object on a form and examining its <TT>DriverName</TT>
property in the Object Inspector. This
property contains a list of the installed
drivers on your system. For instance, all the databases listed earlier in this section
appear in this Property Editor on my system. (Paradox and dBASE tables are accessed
through the driver labeled
"Standard.")</P>
<P>If you are having trouble accessing some or all databases from your system, the
best remedy is usually to do a complete uninstall and a complete reinstall. The local
database systems ought to work automatically out of the
box without any effort on
your part. For instance, I have probably installed BCB 50 times on a variety of systems,
and I have always been able to reach Paradox or dBASE tables immediately after installation.</P>
<P>Users of the BDE should be aware
that BCB ships with a tool called the BDE Configuration
utility. This application can be accessed from the Start menu. You can use this tool
to make sure the BDE itself is running correctly. As a rule, if the BDE is not running,
you will not be able
to access databases from inside of BCB.
<H3><A NAME="Heading5"></A><FONT COLOR="#000077">Database Basics</FONT></H3>
<P>To create a simple database application, start by placing a <TT>TTable</TT>, a
<TT>TDataSource</TT>, and a <TT>TDBGrid</TT>
component on a form, as shown in Figure
8.1.<BR>
<BR>
<A NAME="Heading6"></A><A HREF="08ebu01.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/08/08ebu01.jpg">FIGURE 8.1.</A><FONT COLOR="#000077">
</FONT><TT><I>TTable</I></TT><I>, <TT>TDataSource</TT>, and <TT>TDBGrid</TT> arranged
on a form.</I></P>
<P>Wire these three controls together by completing the following simple steps:
<DL>
<DD><B>1.</B> Connect the <TT>DataSource</TT> property of the <TT>TDBGrid</TT> to
<TT>DataSource1</TT>.<BR>
<BR>
<B>2. </B>Connect the <TT>DataSet</TT> property
of the <TT>TDataSource</TT> control
to <TT>Table1</TT>.
</DL>
<P>After completion of these steps, the three components are hooked together and
can communicate with one another.</P>
<P>Connecting the <TT>TTable</TT> object to a table that resides on
disk is a three-step
process:
<DL>
<DD><B>1. </B>Set the <TT>DatabaseName</TT> property either to a valid alias or,
in the case of Paradox or dBASE, to the subdirectory where your data resides. For
the example currently under discussion, you can
set the <TT>DatabaseName</TT> property
to the <TT>BCDEMOS</TT> alias, which is created by default during BCB's installation.
Alternatively, you could type <TT>c:\CBuilder\demos\data</TT> into the DatabaseName
Property Editor, where you might need
to change some aspects of this path depending
on where you choose to install BCB.<BR>
<BR>
<B>2.</B> Set the <TT>TableName</TT> property to the name of the table you want to
view; for instance, you might choose the <TT>CUSTOMER.DB</TT> table. The
Property
Editor drops down a list of available tables, so there is no need for you to type
anything; the whole job can be done with the mouse.<BR>
<BR>
<B>3.</B> Set the <TT>Active</TT> property, found at the very top of the Object Inspector,
to
<TT>true</TT>.
</DL>
<P>When you are done, the Object Inspector should look as it does in Figure 8.2.<BR>
<BR>
<A NAME="Heading7"></A><A HREF="08ebu02.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/08/08ebu02.jpg">FIGURE 8.2.</A><FONT COLOR="#000077">
</FONT><I>The Object Inspector after connecting
to a table called <TT>Customer</TT>,
using an alias called <TT>BCDEMOS</TT>.</I></P>
<P><BR>
If you have completed all these steps properly, you should now be looking at the
data from the table you chose, as shown in Figure 8.3. To take this process
one step
further, you can compile and run the program and then begin browsing and editing
your data.<BR>
<BR>
<A NAME="Heading8"></A><A HREF="08ebu03.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/08/08ebu03.jpg">FIGURE 8.3.</A><FONT COLOR="#000077">
</FONT><I>Simple form displaying the contents of
<TT>CUSTOMER.DB</TT>.</I></P>
<P><BR>
If you want to simplify the task of browsing through the data in your application,
you can go back into design mode and add the <TT>TDBNavigator</TT> control to the
program. To hook this control into the loop, all
you need to do is set its <TT>DataSource</TT>
property to <TT>DataSource1</TT>. Now you can run the program and begin iterating
through the records with the navigator, as shown in Figure 8.4. In Figure 8.4, most
of the functionality of the
<TT>TDBNavigator</TT> has been turned off by manipulating
its <TT>VisibleButtons</TT> property. For instance, a navigator can automatically
enable you to edit, insert, delete, post, cancel, and refresh. All those capabilities
have been disabled and
hidden in the form shown here by manipulating the <TT>VisibleButtons</TT>
property of <TT>TDBNavigator</TT>.<BR>
<BR>
<A NAME="Heading9"></A><A HREF="08ebu04.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/08/08ebu04.jpg">FIGURE 8.4.</A><FONT COLOR="#000077">
</FONT><I>A simple database program with a
<TT>TDBNavigator</TT> control.</I></P>
<P><BR>
A program like the one described here is found on the CD-ROM that accompanies this
book as <TT>Simple.cpp</TT>. Programs like this are the bread and butter of the database
world. You might even find it
useful to create several programs of this type, just
be to be sure you can do it quickly and easily with little thought. BCB database
programmers work with the <TT>TTable</TT>, <TT>TDataSource</TT>, and <TT>TDBGrid</TT>
objects on a regular basis, and
the basics of their operation should be as easy as
tying your shoe.
<H3><A NAME="Heading10"></A><FONT COLOR="#000077">Naming and Architectural Conventions</FONT></H3>
<P>In this section I lay out a number of conventions that I generally abide by in
programming projects. Before beginning, I should emphasize that these are merely
conventions. There are no hard and fast rules in this area, and you should feel free
to follow my suggestions only to the degree that they suit your taste. In fact, you
will find that I myself do not follow these conventions one hundred percent of the
time, though I generally conform to them when I am not feeling too rushed. I also
have included legacy code in this book in which I did not adopt the techniques that
I
currently believe are best. In some cases, I have updated the legacy code, but
some sample programs still use old conventions.</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -