📄 ch16.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 += '</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"> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"> <TITLE>Teach Yourself Borland Delphi 4 in 21 Days -- Ch 16 -- Delphi Database Architecture</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"></H1></CENTER><CENTER><H1><BR>Teach Yourself Borland Delphi 4 in 21 Days</H1></CENTER><CENTER><P><A HREF="../ch15/ch15.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch17/ch17.htm"><IMGSRC="../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>- 16 -</H1></CENTER><CENTER><H1>Delphi Database Architecture</H1></CENTER><UL> <LI><A HREF="#Heading1">Database Basics</A> <UL> <LI><A HREF="#Heading2">Local Databases</A> </UL> <LI><A HREF="#Heading3">Client/Server Databases</A> <UL> <LI><A HREF="#Heading4">Single-Tier, Two-Tier, and Multitier Database Architecture</A> </UL> <LI><A HREF="#Heading5">The Borland Database Engine</A> <UL> <LI><A HREF="#Heading6">BDE Drivers</A> <LI><A HREF="#Heading7">BDE Aliases</A> <LI><A HREF="#Heading8">Delphi's Built-in Databases</A> <LI><A HREF="#Heading9">SQL Links</A> </UL> <LI><A HREF="#Heading10">Delphi Database Components</A> <UL> <LI><A HREF="#Heading11">The TDataSet Class</A> <LI><A HREF="#Heading12">The Fields Editor</A> <LI><A HREF="#Heading13">The Table Component</A> <LI><A HREF="#Heading14">The Query Component</A> <LI><A HREF="#Heading15">The StoredProc Component</A> <LI><A HREF="#Heading16">The UpdateSQL Component</A> <LI><A HREF="#Heading17">The DataSource Component</A> <LI><A HREF="#Heading18">The Session Component</A> <LI><A HREF="#Heading19">The Database Component</A> </UL> <LI><A HREF="#Heading20">The BatchMove Component</A> <UL> <LI><A HREF="#Heading21">The TField Component</A> </UL> <LI><A HREF="#Heading22">Client/Server Database Components</A> <LI><A HREF="#Heading23">Creating a BDE Alias</A> <UL> <LI><A HREF="#Heading24">Creating an Alias with the BDE Administrator</A> </UL> <LI><A HREF="#Heading25">Creating an Alias Through Code</A> <LI><A HREF="#Heading26">Summary</A> <LI><A HREF="#Heading27">Workshop</A> <UL> <LI><A HREF="#Heading28">Q&A</A> <LI><A HREF="#Heading29">Quiz</A> <LI><A HREF="#Heading30">Exercises</A> </UL></UL><P><HR SIZE="4"><CENTER><H1></H1></CENTER><P>Today you begin to learn about database programming in Delphi. If you are newto database programming, at first glance it might appear overwhelming. Today I'lltry to eliminate any confusion by presenting a clear picture of the labyrinth knownas database programming. First, I'll give you an overview of the Delphi databasearchitecture. After that I'll go over some of the data access components.</P><P>Make no mistake: Database programming is complicated. I'll give you a high-levelview of database programming in Delphi, but I won't attempt to cover every detail.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Not all the concepts and components discussed in this chapter pertain to every version of Delphi. The Professional version of Delphi has more database capabilities than the Standard version. The Client/Server version of Delphi has many more database capabilities than either the Standard or Professional version. <HR></BLOCKQUOTE><H2><A NAME="Heading1"></A>Database Basics</H2><P>Database programming comes with a whole gaggle of buzzwords: <I>BDE</I>, <I>client</I>,<I>server</I>, <I>ODBC</I>, <I>alias</I>, <I>SQL</I>, <I>query</I>, <I>stored</I><I>procedure</I>, and so on. The good news is that it isn't all that bad after youlearn some basics. First, let's take a moment to talk about databases. When you hearthe word <I>database</I>, you probably imagine data stored in table format. The tableprobably contains fields such as FirstName, LastName, and PhoneNumber. These fieldsare filled with data to create individual records in a database file.</P><P>If that's what you envision when you think of a database, you're not too far off,but you aren't exactly correct, either. The term <I>database</I> is used to describean all-encompassing data creation and maintenance system. It is true that a databasecan be as simple as one table. On the other hand, a real-world database can includedozens or even hundreds of tables with thousands or millions of records. These tablescan contain one or more indexes. A complete client/server SQL database solution canalso contain numerous queries and stored procedures. (Don't worry; I'll explain someof these terms later in the chapter.) So as you can see, a database is more thanjust a table with data.</P><P>Speaking of tables, let's quickly cover some table basics. A table consists ofat least two parts: fields and records. <I>Fields</I> are the individual categoriesof data in a table. For example, a table containing an address book would have afield called FirstName, a field called LastName, one called Address, PhoneNumber,and so on. Fields are also referred to as <I>columns</I>. A record, then, is oneperson's complete address: first name, last name, address, and so on. Records arealso called <I>rows</I>.</P><P>A database is just a collection of data, of course, but database tables are oftendisplayed in spreadsheet format. The column headers across the top indicate the fieldnames. Each row in the table contains a complete record. Figure 16.1 shows just sucha database table displayed in grid (or table) format.</P><P><A HREF="javascript:popUp('28671601.gif')"><B>FIGURE 16.1.</B></A><B> </B><I>Atypical database table.</I></P><P><strong>New Term:</strong> The pointer to the current record within a database is calledthe <I>cursor.</I></P><P>The cursor points to the record that will be read if data is requested and therecord that will be updated if any edits are made. The cursor is moved when a userbrowses the database, inserts records, deletes records, and so on.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> When I say the cursor is a pointer, I don't mean it's a pointer in the Object Pascal sense. I merely mean it is an indicator of the current record's position. <HR></BLOCKQUOTE><P><strong>New Term:</strong> A collection of data returned by a database is called a <I>dataset.</I></P><P>A dataset can be more than just the data contained in a table. A dataset can bethe results of a query containing data acquired from many tables. For example, let'ssay you have a database containing names and addresses of your customers, their orders,and the details of each order. This data might be contained in tables named Clients,Orders, and Order Details. Now let's say you request the details of the last 10 ordersplaced by Company X. You might receive a dataset containing information from theClients table, the Orders table, and the Order Details table. Although the data comesfrom several different sources, it is presented to you as a single dataset.</P><P><H3><A NAME="Heading2"></A>Local Databases</H3><P>The simplest type of database is the local database. A <I>local database</I> isa database that resides on a single machine. Imagine that you have a program thatneeds to store a list of names and addresses. You could create a local database tostore the data. This database would probably consist of a single table. The tableis accessed only by your program; no one else has access to it. Any edits made tothe database are written directly to the database. Paradox, dBASE, and Access databasesare usually local databases.</P><P><H2><A NAME="Heading3"></A>Client/Server Databases</H2><P>Another way a database can be implemented is as a <I>client/server database</I>.The database itself is stored and maintained on a file server (the <I>server</I>part of the equation). One or more users (the <I>clients</I>) have access to thedatabase. The users of this type of database are likely to be spread across a network.Because the users are oblivious to one another, more than one might attempt to accessthe database at the same time. This isn't a problem with client/server databasesbecause the server knows how to handle all the problems of simultaneous databaseaccess.</P><P>The users of a client/server database almost never work with the database directly.Instead, they access the database through applications on their local computer. Theseapplications, called <I>client applications</I>, ensure that the users are followingthe rules and not doing things to the database that they shouldn't be. It's up tothe client application to prevent the user from doing something that would damagethe database.</P><BLOCKQUOTE> <P><BR> <HR><B>DATABASE SERVERS</B></P> <P>As long as I am talking about client/server databases, let's take a moment to talk about database servers. Database servers come in several flavors. Some of the most popular include offerings from InterBase (a Borland-owned company), Oracle, Sybase, Informix, and Microsoft. When a company purchases one of these database servers, it also purchases a license that enables a maximum number of users to access the database server. These licensed users are often referred to as <I>seats</I>. Let's say a company buys InterBase and purchases licenses for 50 seats. If that company grows to the point that 75 users require access to the database, that company will have to buy an additional 25 seats to be in compliance with the license. Another way that client/server databases are sold is on a <I>per connection </I>basis. A company can buy a license for 50 simultaneous connections. That company can have 1,000 users of the database, but only 50 can be connected to the database at any one time. The database server market is big business, no question about it. <HR></BLOCKQUOTE><H3><A NAME="Heading4"></A>Single-Tier, Two-Tier, and Multitier Database Architecture</H3><P>Local databases are often called single-tier databases. A <I>single-tier database</I>is a database in which any changes--such as editing the data, inserting records,or deleting records--happen immediately. The program has a more direct connectionto the database.</P><P>In a <I>two-tier database</I>, the client application talks to the database serverthrough database drivers. The database server takes the responsibility for managingconnections, and the client application is largely responsible for ensuring thatthe correct information is being written to the database. A fair amount of burdenis put on the client application to make sure the database's integrity is maintained.</P><P>In a <I>multitier client/server architecture</I>, the client application talksto one or more application servers that, in turn, talk to the database server. Thesemiddle-level programs are called <I>application servers</I> because they servicethe needs of the client applications. One application server might act as a databroker, responding to and handling data requests from the client and passing themon to the database. Another application server might only handle security issues.</P><P>Client applications run on local machines; the application server is typicallyon a server, and the database itself might be on another server. The idea behindthe multitier architecture is that client applications can be very small becausethe application servers do most of the work. This enables you to write what are called<I>thin-client</I> applications.</P><P>Another reason to use a multitier architecture is management of programming resources.The client applications can be written by less experienced programmers because theclient applications interact with the application server that controls access tothe database itself. The application server can be written by more experienced programmerswho know the rules by which the database must operate. Put another way, the applicationserver is written by programmers whose job is to protect the data from possible corruptionby errant client applications.</P><P>Although there are always exceptions, most local databases make use of the single-tierarchitecture. Client/server databases use either a two-tier or a multitier architecture.</P><P>So how does this affect you? Most applications you write with Delphi for use witha client/server database will be client applications. Although you might be one ofthe few programmers given the task of writing server-side or middle-tier applications,it's a good bet that you will write primarily client applications. As an applicationdeveloper, you can't talk directly to these database servers. Let's look next athow a Delphi application talks to a database.</P><P><H2><A NAME="Heading5"></A>The Borland Database Engine</H2><PRE>To enable access to local databases and to client/server databases, Delphi provides the Borland Database Engine (BDE). The BDE is a collection of DLLs and utilities that enables access to a variety of databases.</PRE><P>To talk to client/server databases, you must have the Client/Server version ofDelphi. This version ships with SQL Links drivers used by the BDE to talk to client/serverdatabases. Figure 16.2 shows the relationship between your application, the BDE,and the database.</P><P><BR><A HREF="javascript:popUp('28671602.gif')"><B>FIGURE 16.2.</B></A> <I>Your application,the BDE, and the database.</I></P><P><H3><A NAME="Heading6"></A>BDE Drivers</H3><P>Naturally, database formats and APIs vary widely. For this reason the BDE comeswith a set of drivers that enables your application to talk to several different
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -