Fetching the Data

From RAD Studio
Jump to: navigation, search

Go Up to Using dbExpress Components Index


Once you have specified the source of the data, you must fetch the data before your application can access it. Once the dataset has fetched the data, data-aware controls linked to the dataset through a data source automatically display data values and client datasets linked to the dataset through a provider can be populated with records.

As with any dataset, there are two ways to fetch the data for a dbExpress dataset:

One way is to set the Active property to True, either at design time in the Object Inspector, or in code at run time:

 CustQuery.Active := True;
 CustQuery->Active = true;

Another way is to call the Open method at run time,

 CustQuery.Open;
 CustQuery->Open();

Use the Active property or the Open method with any unidirectional dataset that obtains records from the server. It does not matter whether these records come from a SELECT query (including automatically-generated queries when the CommandType is ctTable) or a stored procedure.

Preparing the dataset

Before a query or stored procedure can execute on the server, it must first be "prepared". Preparing the dataset means that dbExpress and the server allocate resources for the statement and its parameters. If CommandType is ctTable, this is when the dataset generates its SELECT query. Any parameters that are not bound by the server are folded into a query at this point.

Unidirectional datasets are automatically prepared when you set Active to True or call the Open method. When you close the dataset, the resources allocated for executing the statement are freed. If you intend to execute the query or stored procedure more than once, you can improve performance by explicitly preparing the dataset before you open it the first time. To explicitly prepare a dataset, set its Prepared property to True.

 CustQuery.Prepared := True;
 CustQuery->Prepared = true;

When you explicitly prepare the dataset, the resources allocated for executing the statement are not freed until you set Prepared to False.

Set the Prepared property to False if you want to ensure that the dataset is re-prepared before it executes (for example, if you change a parameter value or the SortFieldNames property).

Fetching multiple datasets

Some stored procedures return multiple sets of records. The dataset only fetches the first set when you open it. In order to access the other sets of records, call the NextRecordSet method:

 var
   DataSet2: TCustomSQLDataSet;
   nRows: Integer;
 begin
   DataSet2 := SQLStoredProc1.NextRecordSet;
   ...
 TCustomSQLDataSet *DataSet2 = SQLStoredProc1->NextRecordSet();

NextRecordSet returns a newly created TCustomSQLDataSet component that provides access to the next set of records. That is, the first time you call NextRecordSet, it returns a dataset for the second set of records. Calling NextRecordSet returns a third dataset, and so on, until there are no more sets of records. When there are no additional datasets, NextRecordSet returns nil.

See Also