Using a query component: an overview
From InterBase
Go Up to Working with Queries
To use a query component in an application, follow these steps at design time:
- Place a query component from the InterBase tab of the Tool Palette in a data module, and set its
Name
property appropriately for your application. - Set the
Database
property of the component to the name of theTIBDatabase
component to query. - Set the
Transaction
property of the component to the name of theTIBTransaction
component to query. - Specify a SQL statement in the
SQL
property of the component, and optionally specify any parameters for the statement in theParams
property. For more information, see Specifying the SQL property at design time. - If the query data is to be used with visual data controls, place a data source component from the Data Access tab of the Tool Palette in the data module, and set its
DataSet
property to the name of the query component. The data source component is used to return the results of the query (called a result set) from the query to data-aware components for display. Connect data-aware components to the data source using theirDataSource
andDataField
properties. - Activate the query component. For queries that return a result set, use the
Active
property or theOpen
method. For queries that only perform an action on a table and return no result set, use theExecSQL
method.
Executing the query:
To execute a query for the first time at runtime, follow these steps:
- Close the query component.
- Provide a SQL statement in the
SQL
property if you did not set theSQL
property at design time, or if you want to change the SQL statement already provided. To use the design-time statement as is, skip this step. For more information about setting theSQL
property, see Specifying the SQL statement to execute. - Set parameters and parameter values in the
Params
property either directly or by using theParamByName
method. If a query does not contain parameters, or the parameters set at design time are unchanged, skip this step. For more information about setting parameters, see Setting parameters. - Call
Prepare
to bind parameter values into the query. CallingPrepare
is optional, though highly recommended. For more information about preparing a query, see Preparing a query. - Call
Open
for queries that return a result set, or callExecSQL
for queries that do not return a result set. For more information about opening and executing a query, see Executing a query.
After you execute a query for the first time, then as long as you do not modify the SQL statement, an application can repeatedly close and reopen or re-execute a query without preparing it again. For more information about reusing a query, see Executing a query.