unit CDSDemo;
interface
uses Winapi.Windows, System.SysUtils, System.Classes,
Vcl.Controls,
Vcl.Forms,
Vcl.StdCtrls, Data.DB, Datasnap.DBClient,
Vcl.DBCtrls,
Vcl.DBGrids,
Vcl.Mask,
Vcl.ExtCtrls,
Vcl.Grids, MidasLib;
type
TMainForm =
class(TForm)
DBGrid1: TDBGrid;
DBNavigator1: TDBNavigator;
DBEdit1,DBEdit2: TDBEdit;
DBCheckBox1: TDBCheckBox;
DataSource1: TDataSource;
ClientDataSet1: TClientDataSet;
Button1,Button2,Button3: TButton;
Label1,Label2,Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure DBEdit1Exit(Sender: TObject);
private
{ Private-Deklarationen }
MainPath:
String;
Procedure Init_ClientDataSet;
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
// TClientDataSet wird standardmäsig als XML gespeichert (langsam)
// ich empfehle das Binärformat (schnell ;-) Endung CDS explizit angeben
const CDSsuffix = '
.CDS';
Function DataSetEmpty(aDataSet:TDataSet):Boolean;
begin
Result := TRUE;
if not Assigned(aDataSet)
then Exit;
if not aDataSet.Active
then Exit;
Result := aDataSet.Bof
and aDataSet.Eof;
end;
procedure TMainForm.Button2Click(Sender: TObject);
begin
If DataSetEmpty(ClientDataSet1)
then exit;
ClientDataSet1.DisableControls;
ClientDataSet1.Delete;
if ClientDataSet1.State
in [dsEdit, dsInsert]
then ClientDataSet1.Post;
if ClientDataSet1.RecordCount = 0
then DeleteFile(ClientDataSet1.FileName)
else begin
ClientDataSet1.SaveToFile(ClientDataSet1.FileName, dfBinary);
ClientDataSet1.EnableControls;
end;
ClientDataSet1.EnableControls;
end;
procedure TMainForm.Button3Click(Sender: TObject);
begin
If DataSetEmpty(ClientDataSet1)
then exit;
ClientDataSet1.Close;
DeleteFile(ClientDataSet1.FileName);
Init_ClientDataSet;
end;
procedure TMainForm.DBEdit1Exit(Sender: TObject);
begin
if ClientDataSet1.State
in [dsEdit, dsInsert]
then ClientDataSet1.Post;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
MainPath := ExtractFilePath(Application.ExeName);
Init_ClientDataSet;
end;
Procedure TMainForm.Init_ClientDataSet;
begin
ClientDataSet1.FileName := MainPath + '
ClientDataSet'+CDSsuffix;
if not ClientDataSet1.Active
then DeleteFile(ClientDataSet1.FileName);
If FileExists(ClientDataSet1.FileName)
then ClientDataSet1.Open
else
begin
// eine Definition der Felder kann in der Entwicklungsumgebung
// mit Rechtsklick auf das ClientDataSet erfolgen,
// es öffnet sich der ClientDataSet-Feldeditor
// ich empfehle, die Felder selbst zu definieren := betriebssicherer
// das geht auch zur Laufzeit
with ClientDataSet1.FieldDefs
do
begin
Clear;
Add('
Textfeld', ftString, 25);
Add('
Nummer', ftInteger);
Add('
Checked', ftBoolean);
end;
ClientDataSet1.CreateDataSet;
// bei Bedarf kann schon ein Datensatz angehängt werden
(*with ClientDataSet1 do
begin
Append;
FieldByName('Textfeld').AsString := 'Hello';
FieldByName('Nummer').AsInteger := 3;
FieldByName('Checked').AsBoolean := TRUE;
end;... usw.*)
if ClientDataSet1.State
in [dsEdit, dsInsert]
then ClientDataSet1.Post;
// Speicherung im Binärformat - Endung CDS und 'dfBinary'
ClientDataSet1.SaveToFile(ClientDataSet1.FileName, dfBinary);
end;
DataSource1.DataSet := ClientDataSet1;
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
if DataSetEmpty(ClientDataSet1)
then exit;
ClientDataSet1.Append;
if ClientDataSet1.State
in [dsEdit, dsInsert]
then ClientDataSet1.Post;
end;
end.