Product Documentation

VCL/CLX Developers Guide

Previous Topic

Next Topic

Deleting a Record

To delete a record, retrieve the record to be deleted using one of the search functions discussed in Selecting Records, then call the TDataSet Delete method. It is not necessary to set the fields, except when this is required to retrieve the record (Find method, for instance). The following sequence shows the first record being deleted.

  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. Delete the record using Delete().

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

The following excerpts implement this sequence at run time:

Delphi Example


procedure Form1.DeleteRecord();

begin

try

CtTable1.Table := 'MyTable';

CtTable1.Database := CtDatabase1;

CtTable1.Active := True;

CtTable1.First();

CtTable1.Delete();

except

on E : ECtError do

Application.ShowException(E);

end;

end;

C++ Example


void Form1:: DeleteRecord()

{

try

{

CtTable1->Table = "MyTable";

CtTable1->Database = CtDatabase1;

CtTable1->Active = true;

CtTable1->First;

CtTable1->Delete;

}

catch (ECtError& E)

{

Application->ShowException(&E);

}

}

Use the following procedure to add new data to a c-tree Plus table using the c-treeDB methodology:

  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. Create a record buffer.
  5. Clear the record buffer with a call to Clear().
  6. Set the table fields using one of the SetFieldAs...() methods.
  7. Start a transaction with BeginTransaction() if the table is transaction-controlled.
  8. Use the Write method to add the new record.
  9. Commit the transaction.

Delphi Example


procedure Form1.AddRecord();

var

rec1 : TCtRecord;

begin

try

rec1:=TCtRecord.Create;

rec1.Allocate(CtTable1);

rec1.Clear;

rec1.SetFieldAsString(0,'Bob Nash');

rec1.SetFieldAsInteger(1, 46);

rec1.SetFieldAsString(2,'2300');

rec1.BeginTransaction;

rec1.Write;

rec1.CommitTransaction;

except

on E : ECtError do

Application.ShowException(E);

end;

end;

C++ Example


void Form1:: AddRecord()

{

TCtRecord* rec1;

try

{

rec1 = new TCtRecord();

rec1->Allocate(CtTable1);

rec1->Clear();

rec1->SetFieldAsString(0,"Sverner Knight");

rec1->SetFieldAsUnsigned(1, 52);

rec1->BeginTransaction();

rec1->Write();

rec1->CommitTransaction();

}

catch (ECtError& E)

{

Application->ShowException(&E);

}

}

TOCIndex