A dataset must be in dsInsert mode before an application can add new records. In code, you can use the Insert or Append methods to put a dataset into dsInsert mode if the read-only CanModify property for the dataset is True.
When a dataset transitions to dsInsert mode, it first receives a BeforeInsert event. After the transition to insert mode is successfully completed, the dataset receives first an OnNewRecord event hand then an AfterInsert event. You can use these events, for example, to provide initial values to newly inserted records:
procedure TForm1.OrdersTableNewRecord(DataSet: TDataSet); begin DataSet.FieldByName('OrderDate').AsDateTime := Date; end;
void __fastcall TForm1::OrdersTableNewRecord(TDataSet *DataSet) { DataSet->FieldByName("OrderDate")->AsDateTime = Date(); }
On forms in your application, the data-aware grid and navigator controls can put a dataset into dsInsert state if
Data-aware controls for which inserting is enabled automatically call Post when a user executes any action that changes which record is current (such as moving to a different record in a grid). Otherwise you must call Post in your code.
Post writes the new record to the database, or, if you are caching updates, Post writes the record to an in-memory cache. To write cached inserts and appends to the database, call the dataset's ApplyUpdates method.
Insert opens a new, empty record before the current record, and makes the empty record the current record so that field values for the record can be entered either by a user or by your application code.
When an application calls Post (or ApplyUpdates when using cached updates), a newly inserted record is written to a database in one of three ways:
Append opens a new, empty record at the end of the dataset, and makes the empty record the current one so that field values for the record can be entered either by a user or by your application code.
When an application calls Post (or ApplyUpdates when using cached updates), a newly appended record is written to a database in one of three ways:
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|