Using Unidirectional Result Sets

From RAD Studio
Jump to: navigation, search

Go Up to Using Query-type Datasets

When a query-type dataset returns a result set, it also receives a cursor, or pointer to the first record in that result set. The record pointed to by the cursor is the currently active record. The current record is the one whose field values are displayed in data-aware components associated with the result set's data source. Unless you are using dbExpress, this cursor is bi-directional by default. A bi-directional cursor can navigate both forward and backward through its records. Bi-directional cursor support requires some additional processing overhead, and can slow some queries.

If you do not need to be able to navigate backward through a result set, TQuery and TIBQuery let you improve query performance by requesting a unidirectional cursor instead. To request a unidirectional cursor, set the UniDirectional property to True.

Set UniDirectional before preparing and executing a query. The following code illustrates setting UniDirectional prior to preparing and executing a query:

if not (CustomerQuery.Prepared) then
  CustomerQuery.UniDirectional := True;
  CustomerQuery.Prepared := True;
CustomerQuery.Open;  { returns a result set with a one-way cursor }
if (!CustomerQuery->Prepared)
  CustomerQuery->UniDirectional = true;
CustomerQuery->Prepared = true;
CustomerQuery->Open(); // Returns a result set with a one-way cursor

Note: Do not confuse the UniDirectional property with a unidirectional dataset. Unidirectional datasets (TSQLDataSet, TSQLTable, TSQLQuery, and TSQLStoredProc) use dbExpress, which only returns unidirectional cursors. In addition to restricting the ability to navigate backwards, unidirectional datasets do not buffer records, and so have additional limitations (such as the inability to use filters).

See Also