RTC Forums
March 28, 2024, 10:21:25 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 TDataSet component into a TRtcDataSet object?  (Read 29339 times)
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« on: September 03, 2011, 12:21:17 PM »

Here, you can find an example for a general-purpose function to copy data from a TDataSet descendant component into a TRtcDataSet structure for use with RTC Remote Functions. Please note that you might have to update the RTC_DB2FIELD_TYPE declaration depending on the Delphi version you are using, or chance the way you are mapping Delphi database field types to RTC database field types ...

Code:
uses
  DB, rtcInfo;

{$include rtcDefs.inc}

const
  RTC_DB2FIELD_TYPE: array[TFieldType] of TRtcFieldTypes =
    ( ft_Unknown, ft_String, ft_Smallint, ft_Integer, ft_Word,
      ft_Boolean, ft_Float, ft_Currency, ft_BCD, ft_Date, ft_Time, ft_DateTime,
      ft_Bytes, ft_VarBytes, ft_AutoInc, ft_Blob, ft_Memo, ft_Graphic, ft_FmtMemo,
      ft_ParadoxOle, ft_DBaseOle, ft_TypedBinary, ft_Cursor, ft_FixedChar, ft_WideString,
      ft_Largeint, ft_ADT, ft_Array, ft_Reference, ft_DataSet
      {$IFNDEF IDE_0}
      ,ft_OraBlob, ft_OraClob, ft_Variant,
       ft_Interface, ft_IDispatch,ft_Guid
        {$IFNDEF IDE_1}
        , ft_TimeStamp, ft_FMTBcd
          {$IFDEF IDE_2006up}
            , ft_WideString, ft_WideString, ft_TimeStamp, ft_Variant
          {$ENDIF}
        {$ENDIF}
      {$ENDIF} );

procedure DelphiDataSetToRtc(DelphiDS:TDataSet; rtcDS:TRtcDataSet; ClearFieldDefs:boolean=True);
  var
    fdef:TFieldDef;
    flds:integer;
    fldname:string;
    field:TField;
  begin
  if ClearFieldDefs then
    begin
    rtcDS.Clear;
    for flds:=0 to DelphiDS.FieldCount-1 do
      begin
      fdef:=DelphiDS.FieldDefs.Items[flds];
      fldname:=fdef.Name;
      rtcDS.SetField(fldname,
                           RTC_DB2FIELD_TYPE[fdef.DataType],
                           fdef.Size,
                           fdef.Required);
      end;
    end;

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

DISCLAIMER:

Even though the function shown 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 extract your data from a TDataSet descendant component and copy that data to a TRtcDataSet component, you should use the code example provided above, give it a different name and modify your version to correctly copy 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.