When a client dataset applies its updates, the provider determines how to handle writing the insertions, deletions, and modifications to the database server or source dataset. When you use TClientDataSet with an external provider component, you can use the properties and events of that provider to influence the way updates are applied. These are described in Responding to client update requests.
When the provider is internal, however, as it is for any client dataset associated with a data access mechanism, you can't set its properties or provide event handlers. As a result, the client dataset publishes one property and two events that let you influence how the internal provider applies updates.
procedure TForm1.SimpleDataSet1BeforeUpdateRecord(Sender: TObject; SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind; var Applied Boolean); var SQL: string; Connection: TSQLConnection; begin Connection := (SourceDS as TSimpleDataSet).Connection; case UpdateKind of ukModify: begin { 1st dataset: update Fields[1], use Fields[0] in where clause } SQL := Format(UpdateStmt1, [DeltaDS.Fields[1].NewValue, DeltaDS.Fields[0].OldValue]); Connection.Execute(SQL, nil, nil); { 2nd dataset: update Fields[2], use Fields[3] in where clause } SQL := Format(UpdateStmt2, [DeltaDS.Fields[2].NewValue, DeltaDS.Fields[3].OldValue]); Connection.Execute(SQL, nil, nil); end; ukDelete: begin { 1st dataset: use Fields[0] in where clause } SQL := Format(DeleteStmt1, [DeltaDS.Fields[0].OldValue]); Connection.Execute(SQL, nil, nil); { 2nd dataset: use Fields[3] in where clause } SQL := Format(DeleteStmt2, [DeltaDS.Fields[3].OldValue]); Connection.Execute(SQL, nil, nil); end; ukInsert: begin { 1st dataset: values in Fields[0] and Fields[1] } SQL := Format(InsertStmt1, [DeltaDS.Fields[0].NewValue, DeltaDS.Fields[1].NewValue]); Connection.Execute(SQL, nil, nil); { 2nd dataset: values in Fields[2] and Fields[3] } SQL := Format(InsertStmt2, [DeltaDS.Fields[2].NewValue, DeltaDS.Fields[3].NewValue]); Connection.Execute(SQL, nil, nil); end; end; Applied := True; end;
void __fastcall TForm1::SimpleDataSet1BeforeUpdateRecord(TObject *Sender, TDataSet *SourceDS, TCustomClientDataSet *DeltaDS, TUpdateKind UpdateKind, bool &Applied) { TSQLConnection *pConn := (dynamic_cast<TSimpleDataSet *>(SourceDS)->Connection); char buffer[256]; switch (UpdateKind) case ukModify: // 1st dataset: update Fields[1], use Fields[0] in where clause sprintf(buffer, UpdateStmt1, DeltaDS->Fields->Fields[1]->NewValue, DeltaDS->Fields->Fields[0]->OldValue); pConn->Execute(buffer, NULL, NULL); // 2nd dataset: update Fields[2], use Fields[3] in where clause sprintf(buffer, UpdateStmt2, DeltaDS->Fields->Fields[2]->NewValue, DeltaDS->Fields->Fields[3]->OldValue); pConn->Execute(buffer, NULL, NULL); break; case ukDelete: // 1st dataset: use Fields[0] in where clause sprintf(buffer, DeleteStmt1, DeltaDS->Fields->Fields[0]->OldValue); pConn->Execute(buffer, NULL, NULL); // 2nd dataset: use Fields[3] in where clause sprintf(buffer, DeleteStmt2, DeltaDS->Fields->Fields[3]->OldValue); pConn->Execute(buffer, NULL, NULL); break; case ukInsert: // 1st dataset: values in Fields[0] and Fields[1] sprintf(buffer, UpdateStmt1, DeltaDS->Fields->Fields[0]->NewValue, DeltaDS->Fields->Fields[1]->NewValue); pConn->Execute(buffer, NULL, NULL); // 2nd dataset: values in Fields[2] and Fields[3] sprintf(buffer, UpdateStmt2, DeltaDS->Fields->Fields[2]->NewValue, DeltaDS->Fields->Fields[3]->NewValue); pConn->Execute(buffer, NULL, NULL); break; }
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|