Delphi: using TClientDataset as an in-memory dataset

前端 未结 11 2183
无人及你
无人及你 2020-12-14 08:00

According to this page, it\'s possible to use TClientDataset as an in-memory dataset, completely independent of any actual databases or files. It describes how

相关标签:
11条回答
  • 2020-12-14 08:32

    For me this was caused by a midas.dll mismatch. I fixed it by adding MidasLib to the main program´s uses clause (thus linking the library statically). More info here: http://codeverge.com/embarcadero.datasnap/tclientdataset-createdataset-failing-wit/1097715 and here: http://edn.embarcadero.com/article/29297

    0 讨论(0)
  • 2020-12-14 08:34

    At runtime you can use table.CreateDataset or if this is on a design surface you can right click on the CDS and click create dataset. You need to have specified columns/types for the CDS before you can do this though.

    0 讨论(0)
  • 2020-12-14 08:36

    This is a corrected working code mentioned by OP in the first post. You get a memory table from a TClientDataset shown in DBGrid.

    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, DB, DBClient, Grids, DBGrids, StdCtrls, MidasLib;
    
    type
      TForm1 = class(TForm)
        MemTable: TClientDataSet;
        Button1: TButton;
        Button2: TButton;
        DBGrid1: TDBGrid;
        DataSource1: TDataSource;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      i: word;
    begin
      MemTable.DisableControls;
      for i := 1 to 20000 do
      begin
        MemTable.Append;
        MemTable.FieldByName('ID').AsInteger       := i;
        MemTable.FieldByName('Status').AsString    := 'Code'+IntToStr(i);
        MemTable.FieldByName('Created').AsDateTime := Date();
        MemTable.FieldByName('Volume').AsFloat     := Random(10000);
        MemTable.Post;
      end;
      MemTable.EnableControls;
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      MemTable.IndexFieldNames := 'Volume';
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      MemTable.FieldDefs.Add('ID',      ftInteger, 0, False);
      MemTable.FieldDefs.Add('Status',  ftString, 10, False);
      MemTable.FieldDefs.Add('Created', ftDate,    0, False);
      MemTable.FieldDefs.Add('Volume',  ftFloat,   0, False);
      MemTable.CreateDataSet;
    end;
    
    end.
    
    0 讨论(0)
  • 2020-12-14 08:41

    You can use table.CreateDataSet

    0 讨论(0)
  • 2020-12-14 08:43

    Graveyard stones below for some libre components

    In times of Delphi 5/ Delphi 7 there were initiatives to make any object with published properties (more accurately - array or some collection of those) into a database. On Torry.net those are CollectionDataSet and Object DataSet Years before LINQ and such. But since DB-VCL code is little documented and is spaghetti since 16-bit Delphi 1.0 - those have no development.

    There is also callback-based (events-based) Snap Object Dataset, not so outdated. Though it leaves too much IMHO on developers shoulders.

    TDBF.sf.net table had in-memory mode, but was early removed. TDBF is dead also.

    rxLib/JediVCL has MemoryDataset. Though rxLib target was source-level compatibility since 16-bit Delphi 1 up to Delphi 5. That crippled the code much. In JVCL it had some attention and removing of aging code, but still is half-baked when needed some deeper than trivial usage.

    There are also free-for-personal DCU components like SQLMemoryTable, not for recent releases though. I wonder if Firebird Embedded / SQLite could be used to create in-memory table without using system-wide hacks like RAMdrive :-)

    0 讨论(0)
  • 2020-12-14 08:44

    My preference is to actually manage the dataset as XML. You can use the designer tools to create the basic structure and then save it to disk. This allows it to be managed outside of the executable, compiled in as a resource, or separately managed in version control.

    When doing it in this manner you can use LoadFromFile/Stream and the Save variants. Remember to make proper use of LogChanges and MergeChangeLog depending on your usage.

    0 讨论(0)
提交回复
热议问题