RTC Forums
December 03, 2024, 06:16:13 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: How to copy data from a TRtcDataSet object into a TDataSet component?  (Read 23016 times)
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« on: September 03, 2011, 12:19:34 PM »

Below, you will find an example for a general-purpose function to copy data from a TRtcDataSet structure into a TDataSet descendant component. Please note that you might have to update the RTC_FIELD2DB_TYPE declaration depending on the Delphi version you are using, or change the way you are mapping RTC database field types to Delphi database field types to make it more Delphi version independent ...

Code:
uses
  DB, DBClient, rtcInfo;

{$include rtcDefs.inc}

const
  RTC_FIELD2DB_TYPE: array[TRtcFieldTypes] of TFieldType =
    ( ftUnknown, ftString, ftSmallint, ftInteger, ftWord,
      ftBoolean, ftFloat, ftCurrency, ftBCD, ftDate, ftTime, ftDateTime,
      ftBytes, ftVarBytes, ftAutoInc, ftBlob, ftMemo, ftGraphic, ftFmtMemo,
      ftParadoxOle, ftDBaseOle, ftTypedBinary, ftCursor, ftFixedChar,
      {$IFDEF IDE_1}
        ftString, // TDataSet in D4 doesn't really support WideString
      {$ELSE}
        ftWideString,
      {$ENDIF}
      ftLargeint, ftADT, ftArray, ftReference, ftDataSet
      {$IFNDEF IDE_1}
        ,ftOraBlob, ftOraClob, ftVariant, ftInterface,
         ftIDispatch,ftGuid, ftTimeStamp, ftFMTBcd
      {$ELSE}
        ,ftBlob, ftMemo, ftString, ftBlob, ftBlob,
        ftString, ftDateTime, ftBcd
      {$ENDIF});

procedure RtcDataSetToDelphi(rtcDS:TRtcDataSet; DelphiDS:TDataSet; ClearFieldDefs:boolean=True);
  var
    flds:integer;
    fldname:string;
    field:TField;
  begin
  if ClearFieldDefs then
    begin
    DelphiDS.Active:=False;
    DelphiDS.FieldDefs.Clear;
    for flds:=0 to rtcDS.FieldCount-1 do
      begin
      fldname:=rtcDS.FieldName[flds];
      DelphiDS.FieldDefs.Add(fldname,
                             RTC_FIELD2DB_TYPE[rtcDS.FieldType[fldname]],
                             rtcDS.FieldSize[fldname],
                             rtcDS.FieldRequired[fldname]);
      end;
    if DelphiDS is TClientDataSet then
      TClientDataSet(DelphiDS).CreateDataSet;
    end;

  if not DelphiDS.Active then
    DelphiDS.Active:=True;

  rtcDS.First;
  while not rtcDS.EOF do
    begin
    DelphiDS.Append;
    for flds:=0 to rtcDS.FieldCount-1 do
      begin
      fldname:=rtcDS.FieldName[flds];
      field:=delphiDS.FindField(fldname);
      if assigned(field) then
        field.Value:=rtcDS.Value[fldname];
      end;
    DelphiDS.Post;
    rtcDS.Next;
    end;
  end;

DISCLAIMER:

Even though the function provided above should work for most field types, it can NOT automatically recognize and translate all field types provided by all Database Access components. This is especially true for Memo and BLOB fields. To correctly copy your data from a TRtcDataSet object to a TDataSet descendant component, you should use the function provided above as your starting point, give it a different name and modify it to work correctly with all DB Field types you are using with Database Access components of your choice.

Best Regards,
Danijel Tkalcec
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.022 seconds with 17 queries.