Using components

Using components

There are three Mediator visual components for Delphi:

TMedConnection - the connection component which is similar to BDE TDatabase component.

This component is responsible for establishing and maintaining connection to Mediator server. It is also used for interacting with xHarbour kernel. For more information about xHarbour see xHarbour

TMedTable - component similar to BDE TTable - use it for interacting with Mediator-managed tables and DBF tables managed by xHarbour kernel.

TMedQuery - component similar to BDE TQuery - use it when you wish to directly access underlying SQL database by using SQL command or query.

This components behavior is wery similar to the corresponding BDE components. When in doubt about certain functionality, please refer to BDE component documentation.

Depending on your requirements you will create and configure components either programatically or via Delphi IDE and property editor. After installation, component icons are available on the Mediator tab of the IDE components palette. To drop a component on the form just double click its icon. Then, edit its properties to suit your needs.

Here are general rules for using Mediator components for Delphi

1. Create and configure exactly one TMedConnection component instance in your application. Do it before creating any TMedTable instances. If using IDE, drop TMedConnection component on the form before any TMedTable components to ensure proper creation order. TMedConnection instance is required even if you use only DBF tables.

2. In most cases you will create one instance of TMedTable component for each DBF or Mediator table you access. It is possible, however, to use the single instance of TMedTable component to sequentially open and access many DBF and Mediator tables.

3. Use TMedQuery component instance when you work with Mediator server and you wish to execute SQL query against the SQL database sitting behind Mediator server.

Below simple example illustrating how to programmatically open DBF table.

var

cn: TMedConnection;

tab: TMedTable;

begin

cn := TMedConnection.Create(nil); // create connection instance

cn.ConnType := ctDBF; // only DBF files will be accessed via this connection

cn.Connected := true; // open it (use cn.Open alternatively)

tab := TMedTable.Create(nil); // create table instance

tab.MedConnection := cn; // assign connection (important!)

tab.TableType := ttDBFNTX; // determine RDD driver which should be used (DBFNTX in this case)

tab.TableName := 'c:\temp\test.dbf'; // specify table name

tab.Active := true; // open table

// write number of records

writeln('Table ',tab.TableName,' contains ',ds.RecordCount, ' records');

// go top

ds.First;

// write first record

writeln(ds.RecNo,' ',ds['field1'],' ',ds['field2'],' ',ds['field3']);

tab.Active := false; // close table

tab.Free; // free table instance

cn.Connected := false; // close connection

cn.Free; // free conection instance

end;