TMedQuery Properties Methods Events

TMedQuery is a TMedDataset descendant.

Unit

MedDataset

Description

TMedQuery component is similar in its functionality to BDE TQuery component.

When working with Mediator server TMedQuery component can be used to send SQL queries and commands to SQL database working behind Mediator. It cannot be used without Mediator server.

The same instance of TMedQuery can be used to sequentially execute many queries and commands. Use TMedQuery component for the following purposes:

·Executing of the non-query SQL statements

·Executing SQL queries

·Specifying parameters for SQL queries and commands

·Navigating through SQL query result

·Creating Mediator table containing query result

Instance initialization

At least two properties should be set before executing SQL command or query. These are:

MedConnection - set it to previously created instance of the TMedConnection class

SQL - text of the SQL query or command

By default, query is opened as read-only and forward-only (UniDirectional). Some frequently used properties which change this behavior are:

Scrollable - set if you wish to navigate through query result in both directions

Permanent - set if you wish to store query results as Mediator table which you can later open with TMedTable component. Permanent query is always scrollable. In order to execute permanent query you need to set QueryName property which will determine the name of the new table created from query result.

When initial property values are set, execute SQL command by using ExecSQL method or open query by assigning true to Active property. Make sure TMedConnection instance assigned to MedConnection property is connected before executing command or query. When ready with the query, close it by assigning false to Active property.

Error handling

When working with TMedTable or TMedQuery components, two kind of exceptions can be reported. EMedDataSetError exception is rised if a problem was detected on the VCL component layer. Examine exception object ErrorCode property and ERR_DS_... constants defined in MedDataset.prg to learn more about this kind of errors. Another exception class is EHbError. EHbError exception is raised if the problem was detected in low level interface, most likely while interacting with xHarbour kernel. Both EMedConnectionError and EHbError are descendants of the EDatabaseError class.

Sample TMedQuery initialization for executing command

var

cn: TMedConnection;

qry: TMedQuery;

begin

// TMedConnection initialization here

// . . .

qry := TMedQuery.Create(Nil);

qry.MedConnection := cn; // set connection

qry.SQL.Add('update TEST set FN = :value'); // set SQL

qry.ParamByName('value').Value := 7; // set param value

try

qry.ExecSQL; // execute SQL

writeln(qry.RowsAffected,' records updated'); // report num. of upd. recs

except

writeln('Command failed');

Exit;

end;

qry.Free;

end;

Sample TMedQuery initialization for executing scrollable query

var

cn: TMedConnection;

qry: TMedQuery;

begin

// TMedConnection initialization here

// . . .

qry := TMedQuery.Create(Nil);

qry.MedConnection := cn; // set connection

qry.SQL.Add('select * from TEST where FN = :value'); // set SQL

qry.ParamByName('value').Value := 7; // set param value

qry.Scrollable := true; // we need scrollable query

try

qry.Active := true; // open (execute) query

except

writeln('Query execution failed');

Exit;

end;

qry.Active := false; // close query

qry.Free;