Using TQuery (Procedure)

From RAD Studio
Jump to: navigation, search

Go Up to How To Perform Database Procedures

TQuery is a query-type dataset that encapsulates an SQL statement and enables applications to access the resulting records.

To use TQuery

  1. Choose File > New > Other. The New Items dialog appears.
  2. In the New Items dialog, select Delphi Projects and double-click VCL Forms Application. The Windows Designer displays.
  3. Associate the dataset with database and session connections.
  4. Create heterogeneous queries.
  5. Obtain an editable result set.
  6. Update read-only result sets.

To associate a dataset with database and session connections

  1. From the BDE category of the Tool Palette, drag a TDatabase component to the form.
  2. Drag a TSession component to the form.
  3. Set the DatabaseName property of the TDatabase component to associate a BDE-enabled dataset with a database. For the TDatabase component, database name is the value of the DatabaseName property of the database component.
  4. Specify a BDE alias as the value of DatabaseName if you want to use an implicit database component and the database has a BDE alias.

    Note: A BDE alias represents a database plus configuration information for that database. The configuration information associated with an alias differs by database type (Oracle, Sybase, InterBase, Paradox, dBASE, and so on).

  5. In the Object Inspector, set the DatabaseName to specify the directory where the database tables are located if you want to use an implicit database component for a Paradox or dBASE database.
  6. Use the default session to control all database connections in your application.
  7. Set the SessionName property of the TSession component to associate your dataset with an explicitly created session component.

Note: Whether you use the default session or explicitly specify a session using the SessionName property, you can access the session associated with a dataset by reading the DBSession property. If you use a session component, the SessionName property of a dataset must match the SessionName property for the database component with which the dataset is associated.

To create mixed queries

  1. Define separate BDE aliases for each database accessed in the query using the BDE Administration tool or the SQL explorer.
  2. Leave the DatabaseName property of the TQuery component blank. The names of the databases used will be specified in the SQL statement.
  3. Set the SQL property to the SQL statement you want to execute.
  4. Precede each table name in the statement with the BDE alias for the database of the table, enclosed in colons. This whole reference is then enclosed in quotation marks.
  5. Set the Params property to any parameters for the query.
  6. Write a Prepare method to prepare the query for execution prior to executing it for the first time.
  7. Write an Open or ExecSQL method depending on the type of query you are executing.
  8. Use a TDatabase component as an alternative to using a BDE alias to specify the database in a mixed query.
  9. Configure the TDatabase to the database, set the TDatabase. DatabaseName to an unique value, and use that value in the SQL statement instead of a BDE alias name.

To obtain an editable result set

  1. Set RequestLive property of the TQuery component to True.
  2. If the query contains linked fields, treat the result set as a read-only result set, and update it.

If an application requests a live result set, but the SELECT statement syntax does not allow it, the BDE returns either a read-only result set for queries made against Paradox or dBASE, or an error code for SQL queries made against a remote server.

To update read-only result sets

  1. If all updates are applied to a single database table, indicate the underlying table to update in an OnGetTableName event handler.
  2. Set the query's UpdateObject property to the TUpdateSQL object you are using to have more control over applying updates.
  3. Set the DeleteSQL, InsertSQL, and ModifySQL properties of the update object to the SQL statements that perform the appropriate updates for your query's data.

If you are using the BDE to cache updates, you must use an update object.

See Also