[ Pobierz całość w formacie PDF ]
.Strings = ( select Max(Salary) from Employee )endIn a client/server application, using the Database component is almost mandatory, as it isrequired to define connectivity and login parameters (the user name and password, as youcan see in the Params property above) and to handle transactions.Keep in mind that the Database component establishes a connection with the RDBMS,representing one of the clients of the system.As such, on most servers it requires a license,and your organization is typically paying for a fixed number of licenses.If the same applica-tion or the same computer uses multiple connections to the server, it can count as multipleclients! Fortunately, by setting the KeepConnection property of the Database component,you can specify whether to keep the database connection active even when there is no activeDataSet component using the connection.If your program can fetch some data and thenoperate on it locally, disconnecting from the server might help you conserve licenses.BDE Table and Query Components in Client/ServerIn Delphi there are two BDE components you use to access an existing database table: Tableand Query.When building client/server applications, programmers tend to use the Querycomponent exclusively, but that is certainly not mandatory, and there are cases in whichusing the simpler Table component has no drawback.Here s a quick look at the pros andcons of both components:" While the Table component should not be used to access a large table, it can work per-fectly well with a small lookup table.By opening a Table component, you don t transferthe entire content of the table to the local machine; the data is moved only when youaccess specific records." Consider also that with the Table component, the BDE asks the server first for thetable structure and then for the table data.These two steps are necessary for setting upthe proper internal structures of the BDE, and they are not executed by the Querycomponent.If you activate the BDE s Schema Caching feature, the logical structure ofthe table will be kept locally, saving this extra step.Of course, this might create prob-lems if the logical structure of the table changes on the server." One problem with the Table component is that the BDE mimics a bidirectional cursorby caching the data locally.With a Query component, instead, you can specify whetheryou want this caching or not with the Unidirectional property.Copyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com2874c14.qxd 7/2/01 4:33 PM Page 611Client/Server with the BDE 611" Another point to consider is that you can generally edit the result of a simple query, send-ing the data back to the SQL server.This is accomplished by setting the RequestLiveproperty to True.For more complex queries, however, you ll need to use an UpdateSQLcomponent, something we ll discuss later in this chapter." When trying to minimize the data moved between the server and the client, you needto consider the size of each record as well as the total number of them.When you selectonly a few fields with a query, only part of the data is considered.A Table component,instead, always entails transferring the entire record to the local machine, even if you vefiltered out some fields using the Fields editor.The same problem takes place when youask for a live query (by setting the RequestLiveproperty).In this case, the BDE needs tosee the entire record in order to send back the proper update commands.This meansthat selecting all the records of a table with a live query is equivalent to using the Tablecomponent." The Query component is not limited to select SQL statements; you can also use it toinsert or delete records.When the Query component returns a dataset, you generallyactivate it with the Open method (or with the equivalent operation, setting the Activeproperty to True).When the Query component is used to perform an operation on theserver, you activate it by calling the ExecSQL method.Using Table and Query FiltersOne way to limit the amount of data returned by a table is to filter it.Using the Filter prop-erty of the Table component, you can specify a condition similar to the where clause of aquery.When you work with local databases, the filter is applied by the BDE, but with a SQLserver, the BDE passes the condition to the server in the query generated for the table.Thismakes filtered tables very portable between local and client/server applications.WARNINGThe situation is different if you filter the records in the Pascal code, using the OnFilterRecordevent.In this case, all the records are sent to the client application, which does its own customfiltering.If you use a filter with a Query component, the filtering operation will always be performedlocally by the BDE, even when you are working with a SQL server.In this case, the BDE asksthe server for the entire result set of the query.This would be reasonable only when the userof the application changes the filtering condition often.For a query, only the local filter willbe modified, and the data in the local cache will be used.For a table, the BDE will generate anupdated query to be executed.Copyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com2874c14.qxd 7/2/01 4:33 PM Page 612612 Chapter 14 " Client/Server ProgrammingLive Queries and Cached UpdatesWhen working with local data, it is very common to use grids and other visual controls, editthe data, and send it back to the database.We ve already seen that using a DBGrid mightcause problems when working with an RDBMS, as moving on the grid might send numerousdata requests to the server, creating a huge amount of network traffic.When you use the Query component to connect to some data, you cannot edit the dataunless its RequestLive property is set to True.If you are working with local tables, the queryis always elaborated by the BDE with the Local SQL engine.The BDE will allow for a livequery only if it is quite simple: All joins should be outer joins; there cannot be a distinctkey; there can be no aggregation, no group by or having clause, no subqueries, and no orderby unless supported by an index; and there are other rules you can find in Delphi s Help.If you are working with a SQL server, setting a live query will put the BDE in control ofthe query, instead of the server.When connected to a SQL server, a live query behaves like aTable component.(So it makes sense to use the table anyway, in these cases.)TIPMost SQL servers, including InterBase, allow you to define updateable views based on theresult of a selectstatement that the Local SQL engine of the BDE won t consider updateable.Then you can simply hook a Table component to the view, letting the SQL server do the workand bypassing the Local SQL engine of the BDE.If the BDE determines that the dataset cannot be updated, it sets the CanModify propertyto False.The DataSource component checks this value before allowing an editing operation.A solution to this problem is to avoid the use of data-aware controls, as discussed in the lastchapter, and use specific SQL queries to update, insert, and delete records
[ Pobierz całość w formacie PDF ]