Einzelnen Beitrag anzeigen

Benutzerbild von newIndy
newIndy

Registriert seit: 18. Dez 2018
29 Beiträge
 
Delphi XE3 Professional
 
#3

AW: Beispiel Projekt gesucht, Midaslib/ClientDataSet

  Alt 21. Mär 2022, 08:34
Da gibt es wirklich nicht soo viel im Netz, aber ich kann das Buch 'Delphi in Depth: ClientDataSets' von 'Cary Jensen' sehr empfehlen. Es ist das beste, was ich über ClientDataSets gefunden habe. Für den Anfang habe ich kleines Beispiel zusammengeklickt.
Delphi-Quellcode:
object MainForm: TMainForm
  Left = 0
  Top = 0
  Caption = 'Demo for TClientDataSet'
  ClientHeight = 251
  ClientWidth = 512
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 319
    Top = 10
    Width = 182
    Height = 13
    ParentCustomHint = False
    Caption = 'Datensatz anlegen, editieren, l'#246'schen'
    ParentShowHint = False
    ShowHint = False
  end
  object Label2: TLabel
    Left = 324
    Top = 32
    Width = 50
    Height = 13
    ParentCustomHint = False
    AutoSize = False
    Caption = 'Textfeld'
    ParentShowHint = False
    ShowHint = False
    Layout = tlCenter
  end
  object Label3: TLabel
    Left = 324
    Top = 59
    Width = 50
    Height = 13
    ParentCustomHint = False
    AutoSize = False
    Caption = 'Nummer'
    ParentShowHint = False
    ShowHint = False
    Layout = tlCenter
  end
  object DBGrid1: TDBGrid
    Left = 8
    Top = 8
    Width = 297
    Height = 185
    ParentCustomHint = False
    DataSource = DataSource1
    Options = [dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgRowSelect, dgTitleClick, dgTitleHotTrack]
    ParentShowHint = False
    ShowHint = False
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'Tahoma'
    TitleFont.Style = []
  end
  object DBNavigator1: TDBNavigator
    Left = 8
    Top = 199
    Width = 296
    Height = 25
    ParentCustomHint = False
    DataSource = DataSource1
    VisibleButtons = [nbFirst, nbPrior, nbNext, nbLast]
    ParentShowHint = False
    ShowHint = False
    TabOrder = 1
  end
  object DBEdit1: TDBEdit
    Left = 380
    Top = 29
    Width = 100
    Height = 21
    ParentCustomHint = False
    AutoSize = False
    DataField = 'Textfeld'
    DataSource = DataSource1
    ParentShowHint = False
    ShowHint = False
    TabOrder = 2
    OnExit = DBEdit1Exit
  end
  object DBEdit2: TDBEdit
    Left = 380
    Top = 56
    Width = 100
    Height = 21
    ParentCustomHint = False
    AutoSize = False
    DataField = 'Nummer'
    DataSource = DataSource1
    ParentShowHint = False
    ShowHint = False
    TabOrder = 3
  end
  object DBCheckBox1: TDBCheckBox
    Left = 380
    Top = 83
    Width = 100
    Height = 17
    ParentCustomHint = False
    Caption = 'DBCheckBox1'
    DataField = 'Checked'
    DataSource = DataSource1
    ParentShowHint = False
    ShowHint = False
    TabOrder = 4
  end
  object Button1: TButton
    Left = 330
    Top = 106
    Width = 150
    Height = 25
    ParentCustomHint = False
    Caption = 'Datensatz anlegen'
    ParentShowHint = False
    ShowHint = False
    TabOrder = 5
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 329
    Top = 137
    Width = 150
    Height = 25
    ParentCustomHint = False
    Caption = 'Datensatz l'#246'schen'
    ParentShowHint = False
    ShowHint = False
    TabOrder = 6
    OnClick = Button2Click
  end
  object Button3: TButton
    Left = 330
    Top = 168
    Width = 150
    Height = 25
    ParentCustomHint = False
    Caption = 'Alle Daten l'#246'schen'
    ParentShowHint = False
    ShowHint = False
    TabOrder = 7
    OnClick = Button3Click
  end
  object DataSource1: TDataSource
    Left = 248
    Top = 16
  end
  object ClientDataSet1: TClientDataSet
    Aggregates = <>
    Params = <>
    Left = 120
    Top = 16
  end
end
Delphi-Quellcode:
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.
Miniaturansicht angehängter Grafiken
tclientdataset.jpg  
  Mit Zitat antworten Zitat