Product Documentation

VCL/CLX Developers Guide

Previous Topic

Next Topic

Selecting Records

c-treeVCL/CLX has multiple ways to search records. Being a TDataSet() descendent, all the searches in c-tree files using c-treeVCL/CLX can be performed with inherited methods like First() or Locate(), or data controls like DBNavigator, which are based on the TCtTable component.

c-treeVCL/CLX also includes advanced methods to retrieve records using a record object. These methods include record sets and filters, and are faster than TDataSet() methods since they do not deal with data sources and/or Data Aware controls. On the other hand, this is also a limitation since they do not refresh the data automatically in Data Controls.

The description of these methods is presented in the “Component Reference”, in the TCtRecord class. Of particular interest are the search methods First(), Next(), Prev(), Last(), and Find(). To retrieve the information stored in the records, use the set of methods “GetFieldAs...()” (GetFieldAsString(), GetFieldAsSigned(), GetFieldAsFloat(), etc).

No matter what method is chosen to search for a record, a default index must be selected. To select the default index when using the Table or Record, set the property DefaultIndex. The sequence below represents a loop navigating all the records using the c-treeVCL/CLX technique, and includes a default index.

  1. Log on to a c-tree Plus session (refer to “Log On to c-tree”).
  2. Connect to the database (refer to “Connecting to a database”).
  3. Open the table (refer to "Opening a table” and “Closing a table”).
  4. Create a record buffer.
  5. Select the default index to be used in the search.
  6. Locate the first record using First().
  7. Start up a loop.
  8. Follow to the next record.

Delphi Example


procedure Form1.SearchRecords();

var

rec1 : TCtRecord;

aux : String;

begin

try

rec1:=TCtRecord.Create;

rec1.Allocate(CtTable1);

rec1.DefaultIndex:=0;

rec1.First;

repeat

rec1.GetFieldAsString(0, aux);

ListBox1.Items.Add(aux);

until (rec1.Next=False);except

on E : ECtError do

Application.ShowException(E);

end;

end;

C++ Example


void Form1::SearchRecords()

{

TCtRecord* rec1;

String aux;

try

{

rec1 = new TCtRecord();

rec1->Allocate(CtTable1);

rec1->DefaultIndex = 0;

rec1->First();

do

{

rec1->GetFieldAsString(0, aux);

ListBox1->Items->Add(aux);

}

while (rec1->Next());

}

catch (ECtError& E)

{

Application->ShowException(&E);

}

}

Note that the first user-defined index, index number 0, is the default index if no other index is selected before the search. If there are no user-defined indexes on the table, Recbyt, if it exists, is selected as the default index. If not, the rowid is selected as the default index. If the table has no index defined, the search is sequential.

The default indexes RECBYT and ROWID are created by default in the c-tree tables and are used to order the table by record offset or input order. The meaning of the indexes is:

  • ROWID - this unique index represents the ordering of the table by input order. To set this index as the default, the DefaultIndex property should be set to CTDB_ROWID_IDXNO
  • RECBYT - this index represents the offset of the record inside the table. It is used internally to improve backward physical traversal of variable-length records. To set this index as the default, the DefaultIndex property should be set to CTDB_RECBYT_IDXNO.

TOCIndex