tags:

views:

3674

answers:

8

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 to setup the dataset's table structure and how to load data into it at runtime. But when I tried to follow its instructions in D2009, step 4 (table.Open) raised an exception. It said that it didn't have a provider specified.

The entire point of the example on that page is to build a dataset that doesn't need a provider. Is the page wrong, is it outdated, or am I missing a step somewhere? And if the page is wrong, what do I need to use instead to create a completely independent in-memory dataset? I've been using TJvMemoryData, but if possible I'd like to reduce the amount of extra dependencies that my dataset adds into my project.

+4  A: 

Use table.CreateDataSet

Tom
+10  A: 

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.

MikeJ
+5  A: 

If it helps further, here is a piece of code where I created a ClientDataset that is used as an in-memory table:

procedure TfrmPRMain.ConfigureDataset;
begin
  With cdsMain do begin
    FieldDefs.Add('bDelete', ftBoolean);
    FieldDefs.Add('sSource', ftString, 10);
    FieldDefs.Add('iSection', ftInteger);
    FieldDefs.Add('iOrder', ftInteger);
    FieldDefs.Add('sBranch', ftString, 10);
    FieldDefs.Add('sPulseCode', ftString, 10);
    FieldDefs.Add('sCode', ftString, 10);
    FieldDefs.Add('dtWorkDate', ftDate);
    FieldDefs.Add('iWorkWeek', ftInteger);
    FieldDefs.Add('sName', ftString, 50);
    CreateDataSet;
    LogChanges := False;
    Open;
  end;
end;

You can just substitute your own data information and go. Jack

You don't need the call to Open.
Erick Sasse
+3  A: 

The code from this page doesn't work in ANY Delphi version. A call to CreateDataSet already puts the dataset into active state ("opened"). You should use .CreateDataSet OR .Open. Not both.

Use .Open when you want to fetch data from a Provider (via ProviderName property) and .CreateDataSet when you want to populate the Dataset by yourself.

BTW: For an in depth reference about ClientDataSets and its features take a look on excellent Cary Jensen articles on CodeGear Developer Network (read the oldest ones, first)

F.D.Castel
+3  A: 

Don't forget to include MIDAS.DLL in your installation or simply include MidasLib in uses clause. Otherwise using TClientDataSet will raise an error on client's machine. Maybe it's obvious, but I actually forgot this once.

vrad
A: 

for some reason this is not working for me. I perform CreateDataset in design time, but it still crashes application. That is still unknown for me. One warning. DO NOT DO THIS:

XXXClientDataSet.Close;
XXXClientDataSet.Open;

because it will report error. Instead of Open, use

xxxClientDataSet.CreateDataset;

In my application I needed to reset data and load it again, and that again caused error message.

A: 

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.

Ryan VanIderstine
+1  A: 

If you'd like a dependency-free, high quality, and feature rich (not to mention free!) in-memory dataset, I highly recommend kbmMemTable. Does everything TClientDataset does and then some.

Tim Sullivan
Yes, it does have that method.
Tim Sullivan
Impressive feature set. Unfortunately, it won't compile under D2009.
Mason Wheeler
According to their news item, it does support D2009: http://www.components4programmers.com/news/kbmmemtablev_6_00professionalsta.htm
Tim Sullivan
Yeah, under version 6. But the actual download page doesn't seem to have a link for version 6, which doesn't reflect too well on the company...
Mason Wheeler
I'm sure it's an oversight, and a quick email will fix it. C4D has been an active member of the Delphi community for over a decade, so they're not exactly a fly-by-night organization.
Tim Sullivan