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):
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.
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);
}
}