Product Documentation

VCL/CLX Developers Guide

Previous Topic

Next Topic

Connecting to a Database

It is necessary to connect to a database before creating a c-tree Plus table and/or manipulating data stored in a c-tree Plus table. To connect to a database at design time, execute the following steps:

  1. Log on to a c-tree Plus session (refer to “Log On to c-tree”).
  2. Drop a TCtDatabase component on a form.
  3. Set the Database property to the database name.
  4. Set the Session property to the Session component.
  5. Set the Active property to True to connect the database.

The database path is not required in order to connect to the database. This information is stored in the session dictionary.

When the database connection is no longer necessary, disconnect from the database by setting the Active property to False or by calling the Disconnect() method.

To connect to a database at run time, execute the following steps after the TCtDatabase component has been dropped on the form:

  1. Log on to a c-tree Plus session (refer to “Log On to c-tree”).
  2. Set the Database property to the database name.
  3. Set the Session property to the Session component.
  4. Call the method Connect() to connect to the database.

The sequences below represent the run time connection to the database dictionary in Delphi and C++.

Delphi Example


procedure Form1.ConnectToDatabase;

begin

try

CtDatabase1.Database := 'MyData';

CtDatabase1.Session := CtSession1;

CtDatabase1.Connect();

except

on E : ECtError do

Application.ShowException(E);

end;

end;

C++ Example


void Form1::ConnectToDatabase()

{

try

{

CtDatabase1->Database = "MyData";

CtDatabase1->Session = CtSession1;

CtDatabase1->Connect();

}

catch (ECtError& E)

{

Application->ShowException(&E);

}

}

Once again, it is important to disconnect from the database before closing the application. If, for any reason, the user logs off from the session, the database is automatically disconnected. To disconnect a database, either set its Active property to False or call the Disconnect method. It is also possible to disconnect from all active databases at once using the TCtSession DisconnectAll() method.

TOCIndex