Pages

Friday, 7 March 2014

How to Insert, Append, Edit and Delete Rows in a Dataset in Delphi?

How to Insert, Append, Edit and Delete Rows in a Dataset in Delphi?

You can insert, append, edit and delete rows in datasets in Delphi. Following are the methods for performing these operations:

Insert: Add a new record to the dataset at current position
Append: Add a new record to the dataset at the end
Edit: Sets the dataset in the 'edit' mode
Post: Post changes to database
Cancel: Cancel an edit/insert action
Delete: Removes the active record

Inserting/Appending a row in the dataset: The Append and Insert methods allow you to begin the process of adding a row to the dataset. The only difference between these two methods is the Insert method will insert a blank row buffer at the current position in the dataset, and the Append method will add a blank row buffer at the end of the dataset. This row buffer does not exist in the physical datset until the row buffer is posted to the actual dataset using the Post method. If the Cancel method is called, then the row buffer and any updates to it will be discarded. Also, once the row buffer is posted using the Post method it will be positioned in the dataset according to the active index order, not according to where it was positioned due to the Insert or Append methods.

Suppose I have two fields in the dataset "PROGRAMMING_LANGUAGE" and "VERSION". I insert the records in these fields.

with DataSet1 do
begin
  Insert; //Set the dataset in the insert mode
  FieldByName('PROGRAMMING_LANGUAGE').AsString := 'DELPHI'; //Fill in the values
  FieldByName('VERSION').AsString := 'XE4';
  Post;
end;

Same thing with Append method:

with DataSet1 do
begin
  Append; //Set the dataset in the append mode
  FieldByName('PROGRAMMING_LANGUAGE').AsString := 'DELPHI'; //Fill in the values
  FieldByName('VERSION').AsString := 'XE4';
  Post;
end;

Editing a row in the dataset: The Edit method allows you to begin the process of editing an existing row in the dataset. Suppose I want to change the version of Delphi from XE4 to XE5.

with DataSet1 do
begin
  Edit; //Set the dataset in the edit mode
  FieldByName('VERSION').AsString := 'XE5'; //Update the field
  Post;
end;

Delete a row in the dataset: The Delete method allows you to delete an existing row in a dataset. Unlike the Append, Insert, and Edit methods, the Delete method is a one-step process and does not require a call to the Post method to complete its operation. A row lock is obtained when the Delete method is called and is released as soon as the method completes. After the row is deleted the current position in the dataset will be the next closest row based upon the active index order.

The following example shows how to use the Delete method to delete a row in a dataset:

with DataSet1 do
begin
  Delete;
end;

Cancelling an Insert/Append or Edit Operation: You may cancel an existing Insert/Append or Edit operation by calling the Cancel method. Doing this will discard any updates to an existing row if you are editing, or will completely discard a new row if you are inserting or appending. The following example shows how to cancel an edit operation on an existing row:

with DataSet1 do
begin
  Edit; //Set the dataset in the edit mode
  FieldByName('VERSION').AsString := 'XE5'; //Update the field
  Cancel;
end;

No comments:

Post a Comment

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.