Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve independent cloned TADODataSet?

The scenarios is like this:

We have some SQL table. We are performing an SQL query on this table and we have results in TADOQuery object.

var
  qryOryginal, qryClone: TADOQuery;

begin
  //setup all the things here
  qryOryginal.Active := True;
  qryClone.Clone(qryOryginal, ltBatchOptimistic);
  qryOryginal.Delete; //delete in qryOryginal casues that qryClone deletes its record too!
end;

So, after cloning the DataSet my qryClone should hold and independent data(at least I thought so). However, performing Delete on qryOryginal causes the same operation on the qryClone. I don't want that.

Any ideas?

I know I could store the data elsewhere, in TClientDataSet perhaps but I would like to try the above solution first.

Thanks in advance for your time.

like image 233
Wodzu Avatar asked Jan 23 '23 15:01

Wodzu


1 Answers

You can use the recordset of a TADODataSet to clone a TADODataSet.

ds1.Recordset := CloneRecordset(ds2.Recordset);

This version works from Delphi XE. ADOInt is updated with the type library definitions for MDAC 2.8

uses ADOInt, Variants;

function CloneRecordset(const Data: _Recordset): _Recordset;

implementation    

function CloneRecordset(const Data: _Recordset): _Recordset;
var
    newRec: _Recordset;
    stm: Stream;
begin
    newRec := CoRecordset.Create as _Recordset;
    stm := CoStream.Create;
    Data.Save(stm, adPersistADTG);
    newRec.Open(stm, EmptyParam, CursorTypeEnum(adOpenUnspecified),
        LockTypeEnum(adLockUnspecified), 0);
    Result := newRec;
end;

This version must be used for versions of Delphi prior to Delphi XE. ADOR_TLB is generated from msado28.tlb.

uses ADOInt, ADOR_TLB, Variants;

function CloneRecordset(const Data: ADOInt._Recordset): ADOInt._Recordset;

implementation

function CloneRecordset(const Data: ADOInt._Recordset): ADOInt._Recordset;
var
    newRec: ADOR_TLB._Recordset;
    stm: Stream;
begin
    newRec := ADOR_TLB.CoRecordset.Create as ADOR_TLB._Recordset;
    stm := CoStream.Create;
    (Data as ADOR_TLB._Recordset).Save(stm, adPersistADTG);
    newRec.Open(stm, EmptyParam, CursorTypeEnum(adOpenUnspecified),
        LockTypeEnum(adLockUnspecified), 0);
    Result := newRec as ADOInt._Recordset;
end;
like image 101
Mikael Eriksson Avatar answered Jan 29 '23 10:01

Mikael Eriksson