📄 ch16.htm
字号:
record are written to the database when the cursor leaves the record. This is finefor local databases, but it is not a good solution for client/server databases fora variety of reasons. Most often you hear people talk about network traffic beingthe primary reason for using cached updates. Although it is certainly true that cachedupdates help reduce network traffic, the value of cached updates goes far beyondthe issue of network traffic. Let me explain further.</P><P>Many client/server databases return a read-only result set as the result of aquery. One advantage of cached updates is that the client can work with a local copyof a dataset, modify it as needed, and then write the edits to the database all atone time. This is possible because the database server handles updates, insertions,and deletions of records from a read-only dataset. A local database has to lock recordswhen they are being actively edited. When a record is locked, other database userscannot access the record. Using cached updates reduces the time a record is lockedto a very short period of time.</P><P>Another advantage to cached updates is that a user can make several changes toa dataset and then either <I>commit</I> (apply) all changes or <I>rollback</I> (cancel)all changes. This a two-edged sword, however, because if something happens to gowrong on the server when changes are being written to the database, all changes arelost.</P><P>One drawback of cached updates is that several users might be working with thesame record at the same time. It then becomes a race to see who gets the record updatedfirst. In reality, this problem is reduced somewhat by implementing techniques inthe client application that check whether multiple edits have taken place on a record.For example, if Joe tries to post an update to a record, the database and/or clientapplication will notify Joe that Mary has changed the record since Joe initiallyretrieved it from the database. Joe will have to refresh his copy of the datasetto see whether he still needs to modify the record.</P><P><H3><A NAME="Heading13"></A>The Table Component</H3><P>The Table component, represented by the TTable class, provides the quickest andsimplest access to a table. Tables are more than adequate for most single-tier databaseapplications. Usually, you will use the Table component when dealing with local databasesand the Query component when dealing with SQL database servers.</P><P>The TTable class has many properties and methods in addition to those in its ancestorclass, TDataSet. Table 16.4 lists the primary properties of the TTable componentand Table 16.5 lists the primary methods. Remember, these are properties and methodsspecific to TTable and do not include those of TTable's ancestor, TDataSet.</P><P>For the most part, the properties and methods are very intuitive. By that I meanthat you can usually figure out what a property or method does by just looking atits name. It doesn't take a lot to figure out that the LockTable method locks a tablefor an application's specific use and that the UnlockTable method unlocks the tableagain. Likewise, you don't have to have an IQ of 150 to guess what the CreateTable,DeleteTable, and RenameTable methods do. With that in mind, I'm not going to coverevery aspect of every property and method listed here. Instead, let's get on to someof the more interesting aspects of the Table component.</P><P><H4>TABLE 16.4. PRIMARY TTable PROPERTIES.</H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><I>Property</I></TD> <TD ALIGN="LEFT"><I>Description</I></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Exclusive</TD> <TD ALIGN="LEFT">Locks a local table so that only this application can use it.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">IndexDefs</TD> <TD ALIGN="LEFT">Contains information about the table's indexes.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">IndexFieldCount</TD> <TD ALIGN="LEFT">The number of fields that make up the current key.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">IndexFieldNames</TD> <TD ALIGN="LEFT">Used to set the current key by specifying the names of the fields to use for the index.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">IndexFields</TD> <TD ALIGN="LEFT">Used to retrieve information about a specific field in an index.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">IndexName</TD> <TD ALIGN="LEFT">Used to specify a secondary index for a table.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">KeyFieldCount</TD> <TD ALIGN="LEFT">The number of fields to use when searching on partial keys.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">MasterFields</TD> <TD ALIGN="LEFT">The field or fields that should join the master and detail tables.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">MasterSource</TD> <TD ALIGN="LEFT">The table to be used as a master table when this table is used as a detail table.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">ReadOnly</TD> <TD ALIGN="LEFT">Specifies whether this table is read-only.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">TableName</TD> <TD ALIGN="LEFT">The name of the database table.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">TableType</TD> <TD ALIGN="LEFT">The table's type (Paradox, dBASE, or ASCII).</TD> </TR></TABLE><H4>Table 16.5. Primary TTABLE methods.</H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><I>Method</I></TD> <TD ALIGN="LEFT"><I>Description</I></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">AddIndex</TD> <TD ALIGN="LEFT">Creates a new index for the table.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">ApplyRange</TD> <TD ALIGN="LEFT">Applies a range to the dataset. Only records within that the range (determined by SetRangeStart and SetRangeEnd) are available for viewing or editing.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">BatchMove</TD> <TD ALIGN="LEFT">Moves records from a dataset into the table.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CancelRange</TD> <TD ALIGN="LEFT">Removes any ranges currently in effect for the table.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CreateTable</TD> <TD ALIGN="LEFT">Re-creates the table using new information.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">DeleteIndex</TD> <TD ALIGN="LEFT">Deletes a secondary index.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">DeleteTable</TD> <TD ALIGN="LEFT">Deletes a table.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">EmptyTable</TD> <TD ALIGN="LEFT">Deletes all records from the table.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">GetIndexNames</TD> <TD ALIGN="LEFT">Retrieves a list of all indexes for the table.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">GotoKey</TD> <TD ALIGN="LEFT">Moves the cursor to the record indicated by the current key.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">GotoNearest</TD> <TD ALIGN="LEFT">Moves the cursor to the record that most closely matches the current key.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">LockTable</TD> <TD ALIGN="LEFT">Locks a table so that other applications cannot access it.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">RenameTable</TD> <TD ALIGN="LEFT">Renames the table.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">SetKey</TD> <TD ALIGN="LEFT">Enables you to set keys for the dataset.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">SetRange</TD> <TD ALIGN="LEFT">Sets the start and end range for a dataset and applies the range. This method performs the same action as calling the SetRangeStart, SetRangeEnd, and ApplyRange methods.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">SetRangeEnd</TD> <TD ALIGN="LEFT">Sets the end of the range.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">SetRangeStart</TD> <TD ALIGN="LEFT">Sets the beginning of the range.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">UnlockTable</TD> <TD ALIGN="LEFT">Unlocks a table that was previously locked with LockTable.</TD> </TR></TABLE><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> As you have already seen, the DatabaseName property is used to select a BDE alias. For local databases, rather than select an alias from the list, you can enter a directory where database files are located. The TableName property will then contain a list of database tables in that directory. <HR></BLOCKQUOTE><H4>Filters</H4><PRE>A common need of a database application is to filter a table. Before I discuss filters in detail, I want to point out that filters are primarily used on local databases. Filters are rarely used with client/server databases; instead, a SQL query is used to achieve the same effect that filters have on local databases.</PRE><P>So why filter? Consider that you might have a table with thousands of records,but you are interested in displaying or working on only a small subset of the table.Let's say you have a database that contains names and addresses of computer usersall over the world. Your company sells these names and addresses to other companiesthat want to do bulk mailings.</P><P>I call and want to order a mailing list from your company, but I want the listto contain only those computer users who live in Colorado. You could filter yourtable by postal code and generate a list of names with only Colorado addresses. Or,maybe Borland calls you and wants a list of computer users in Great Britain who areprogrammers by occupation. In that case, you could filter by occupation and country,thereby giving only the names and addresses the customer is interested in.</P><P><B>Using Filters in the Table Component  </B>Filters in the Table componentare handled in one of two ways: through the Filter property or the OnFilterRecordevent. Before I discuss these, let me talk about the Filtered property. This propertydetermines whether the table is filtered. If Filtered is True, the table will applythe filter currently in force (either the contents of the Filter property or theresults of the OnFilterRecord event). If Filtered is False, the contents of the Filterproperty are ignored and the OnFilterRecord event is never generated.</P><P>For the Filter property, you implement a field name, a logical operator, and avalue. A filter might look like this:</P><P><PRE>FirstName = `Bob'</PRE><P>This statement, in effect, says, "Show me all records in which the firstname is Bob." Filters can also use the keywords AND, OR, or NOT:</P><P><PRE>CustNo = 1384 AND ShipDate < `1/1/94'</PRE><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> The field name and the logical operators (AND, OR, or NOT) are not case sensitive. The following two filter statements are identical: <HR></BLOCKQUOTE><PRE>CustName = `TurboPower' and ShipDate < `1/1/94'CUSTNAME = `TurboPower' AND SHIPDATE < `1/1/94'</PRE><P>In the case of searching for text, the FilterOptions property determines whetherthe search string is interpreted as case sensitive.</P><P>The following operators can be used in filter statements:</P><P><TABLE BORDER="1"> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><I>Operator</I></TD> <TD ALIGN="LEFT"><I>Use</I></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><</TD> <TD ALIGN="LEFT">Less than</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">></TD> <TD ALIGN="LEFT">Greater than</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">=</TD> <TD ALIGN="LEFT">Equal to</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><></TD> <TD ALIGN="LEFT">Not equal to</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">>=</TD> <TD ALIGN="LEFT">Greater than or equal to</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><=</TD> <TD ALIGN="LEFT">Less than or equal to</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">()</TD> <TD ALIGN="LEFT">Used to specify the evaluation order of compound</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">expressions</TD> <TD ALIGN="LEFT"></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">[]</TD> <TD ALIGN="LEFT">Used around field names containing spaces</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -