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.
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: