Product Documentation

VCL/CLX Developers Guide

Previous Topic

Next Topic

Working with Records

To update (add, update, delete) data in a table, the table must first be created and opened. There are different ways to insert the new (or update current) records: using the TDataSet methodology, or using the c-treeDB methodology, all of them at run time.

Use the following sequence to add new data to a c-tree Plus table using the TDataSet methodology (not the only alternative):

  1. Log on to a c-tree Plus session (please refer to “Log On to c-tree”).
  2. Connect to the database (please refer to “Connecting to a database”).
  3. Open the table ( refer to “Opening a table” and “Closing a table”).
  4. Call the TDataSet Append() or Insert() method.
  5. Set the table fields.
  6. Post the new record.

The following excerpts implement this sequence at run time:

Delphi Example


procedure Form1.AddRecord();

begin

try

CtTable1.Table := 'MyTable';

CtTable1.Database := CtDatabase1;

CtTable1.Active := True;

CtTable1.Append;

CtTable1.FieldByName('Name').AsString:='Bob Nash';

CtTable1.FieldByName('Age').AsInteger:=46;

CtTable1.FieldByName('Salary').AsString:= '2300';

CtTable1.Post;

except

on E : ECtError do

Application.ShowException(E);

end;

end;

C++ Example


void Form1:: AddRecord()

{

try

{

CtTable1->Table = "MyTable";

CtTable1->Database = CtDatabase1;

CtTable1->Active = true;

CtTable1->Append();

CtTable1->FieldByName("Name")->AsString="Bob Nash";

CtTable1->FieldByName("Age")->AsInteger=46;

CtTable1->FieldByName("Salary")->AsString="2300";

CtTable1->Post;

}

catch (ECtError& E)

{

Application->ShowException(&E);

}

}

To update a record (instead of adding a new one), retrieve the record to be updated using one of the search functions discussed in Selecting Records, then call the TDataSet Write method. The modified fields should be set after the record is found and before it is rewritten. The following sequence shows the first record being updated.

  1. Log on to a c-tree Plus session (please refer to “Log On to c-tree”).
  2. Connect to the database (please refer to “Connecting to a database”).
  3. Open the table ( refer to “Opening a table” and “Closing a table”).
  4. Locate the first record using First().
  5. Update the fields that have changed.
  6. Call Post() to update the current record.

Note that the first record is being retrieved without checking which index is used. The searching methods will be discussed in the topic “Selecting Records”. In the current topic, the update is the central point of interest.

The following excerpts implement this sequence at run time. Note that just one of the fields, Age, has been changed from some value to 44.

Delphi Example


procedure Form1.UpdateRecord();

begin

try

CtTable1.Table := 'MyTable';

CtTable1.Database := CtDatabase1;

CtTable1.Active := True;

CtTable1.First();

CtTable1.FieldByName('Age').AsInteger:=44;

CtTable1.Post;

except

on E : ECtError do

Application.ShowException(E);

end;

end;

C++ Example


void Form1:: UpdateRecord()

{

try

{

CtTable1->Table = "MyTable";

CtTable1->Database = CtDatabase1;

CtTable1->Active = true;

CtTable1->First;

CtTable1->FieldByName("Age")->AsInteger=44;

CtTable1->Post;

}

catch (ECtError& E)

{

Application->ShowException(&E);

}

}

TOCIndex