Single Tier Applications

Question:

How do I set up a single tier application using Client Datasets?

Answer:

Currently, the only way to activate a client dataset at design time is to associate it with a provider, to load data from a file, or to assign data from another dataset. Typically, for a single tier application you would create the dataset at runtime using the CreateDataSet method. Here is how you might code the FormCreate and FormDestroy methods:

OnFormCreate:

if FileExists('MYDATA.CDS') then

  ClientDataSet.LoadFromFile('MYDATA.CDS')

else

begin

  ClientDataSet.FieldDefs.Add('Field1', etc.);

  ClientDataSet.FieldDefs.Add('Field2', etc.);

  ClientDataSet.CreateDataset;

end;

 

OnFormDestroy:

if ClientDataSet.Active then

begin

  // Next line is optional,

  // if you don't want undo support you could

  // set LogChanges = False instead.

  ClientDataSet.MergeChangeLog;

  ClientDataSet.SaveToFile('MYDATA.CDS');

end;

Once you've run the app once and created the data file, you can easily load it at design time (from the right-click component menu) if you need to have the dataset active at design time for some reason.