TMedConnection Properties Methods Events

TMedConnection is a TCustomConnection descendant.

Unit

MedDataset

Description

TMedConnection component is similar in its functionality to BDE TDatabase component.

Every application using Mediator components for Delphi requires exactly one instance of TMedConnection component. This component instance should be created before any other Mediator components. TMedConnection component can be used for the following purposes:

·Establishing and maintaining connection to Mediator server

·Establishing and maintaining 'virtual' connection to DBF files

·Defining environmental parameters for DBF and Mediator tables (such as default DBF directory)

·Managing transactions

·Managing all DataSets (TMedTables and TMedQueries) associated with this connection

·Accessing xHarbour kernel functions and parameters

·Executing xHarbour user code contained in DLL files

·Executing non-query SQL commands

Instance initialization

As the absolute minimum, you will need to set ConnType property right after instance creation.

If you wish to work with Mediator server set the following properties which will enable you to connect to server:

MedUser

MedPasswd

MedNodeAddr

MedSocket

MedCS

When working with DBF files it is convenient to set default DBF files directory. This can be done by setting any of the two properties:

DBFPath

Directory

When initial property values are set, establish the connection by assigning true to Connected property. When ready with database access, close connection by assigning false to Connected property.

Error handling

When working with TMedConnection component, two kind of exceptions can be reported. EMedConnectionError exception is rised if a problem was detected on the VCL component layer. Examine exception object ErrorCode property and ERR_CONN_... 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 TMedConenction initialization for DBF-files only

var

cn: TMedConnection;

begin

cn := TMedConnection.Create(Nil);

cn.ConnType := ctDBF; // DBF files only

cn.DBFPath := 'c:\dbf'; // set default DBF directory

try

cn.Connected := true; // activate the connection

except

writeln('Connection failed');

Exit;

end;

// . . .

cn.Connected := false;

cn.Free;

end;

Sample TMedConenction initialization for Mediator and DBF files

var

cn: TMedConnection;

begin

cn := TMedConnection.Create(Nil);

cn.ConnType := ctMEDSRV; // Mediator server and DBF files

cn.DBFPath := 'c:\dbf'; // set default DBF directory

// set additional properties required to connect to Mediator server

cn.MedNodeAddr := '10.1.1.25'; // set Mediator server IP or symbolic name

cn.MedSocket := '19C8'; // set Mediator server listening port

cn.MedUser := 'myaccount'; // set Mediator server or database user name

cn.MedPasswd := 'mypasswd'; // set Mediator server or database user password

cn.MedCS := 'mydatasource'; // set ODBC source or connect string for Oracle

try

cn.Connected := true; // activate the connection

except

writeln('Connection failed');

Exit;

end;

// . . .

cn.Connected := false;

cn.Free;

end;