ThWboard Support-Forum (Archiv)

Ort: / Boardübersicht / Test-Board / Code Test


Seite 1 von 1

bdominik schrieb am 18.10.2002 um 19:18 Uhr

[b]unit[/b] BriefcaseMain;

[i]{ This program demonstrates how to do disconnected briefcase applications
  with ADO.  When the Connected checkbox is unchecked the application is
  switched into offline mode.  If the application is exited at that point
  then the data is persisted to a file on disk (along with any edits to the
  data).  When the application is restarted it will load the persisted data
  if present, otherwise it will fetch the data from the database. }[/i]

[b]interface[/b]

[b]uses[/b]
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, DB, ADODB, Grids, DBGrids, ExtCtrls;

[b]type[/b]
  TForm1 = [b]class[/b](TForm)
    Employees: TADODataSet;
    EmpSource: TDataSource;
    DBGrid1: TDBGrid;
    Connection: TADOConnection;
    Panel1: TPanel;
    ConnectionInd: TCheckBox;
    UpdateButton: TButton;
    RefreshButton: TButton;
    SaveButton: TButton;
    [b]procedure[/b] Form1Create(Sender: TObject);
    [b]procedure[/b] SaveButtonClick(Sender: TObject);
    [b]procedure[/b] Form1CloseQuery(Sender: TObject; [b]var[/b] CanClose: Boolean);
    [b]procedure[/b] UpdateButtonClick(Sender: TObject);
    [b]procedure[/b] ConnectionIndClick(Sender: TObject);
    [b]procedure[/b] RefreshButtonClick(Sender: TObject);
  [b]private[/b]
    DataFileName: [b]string[/b];
  [b]public[/b]
    [b]procedure[/b] LoadData;
    [b]procedure[/b] SaveData;
    [b]procedure[/b] UpdateData;
  [b]end[/b];

[b]var[/b]
  Form1: TForm1;

[b]implementation[/b]

[i]{$R *.dfm}[/i]

[b]const[/b]
  BaseFileName = 'EMPLOYEE.ADTG';

[b]procedure[/b] TForm1.LoadData;
[b]begin[/b]
  DataFileName := ExtractFilePath(Paramstr(0))+BaseFileName;
  [i]{ If a persisted datafile exists, assume we exited in a disconnected
    (offline) state and load the data from the file. }[/i]
  [b]if[/b] FileExists(DataFileName) [b]then[/b]
    Employees.LoadFromFile(DataFileName)
  [b]else[/b]
  [b]begin[/b]
    [i]{ Otherwise establish the connection and get data from the database }[/i]
    ConnectionInd.Checked := True;
    Employees.Open;
  [b]end[/b];
[b]end[/b];

[b]procedure[/b] TForm1.UpdateData;
[b]begin[/b]
  [i]{ Connect to the database and send the pending updates }[/i]
  ConnectionInd.Checked := True;
  Employees.UpdateBatch;
  DeleteFile(DataFileName);
[b]end[/b];

[b]procedure[/b] TForm1.SaveData;
[b]begin[/b]
  [i]{ Persist the data to disk }[/i]
  Employees.SaveToFile(DataFileName, pfADTG);
[b]end[/b];

[b]procedure[/b] TForm1.Form1Create(Sender: TObject);
[b]begin[/b]
  Connection.ConnectionString := 'FILE NAME=' + DataLinkDir + '\DBDEMOS.UDL';
  LoadData;
[b]end[/b];

[b]procedure[/b] TForm1.SaveButtonClick(Sender: TObject);
[b]begin[/b]
  SaveData;
[b]end[/b];

[b]procedure[/b] TForm1.UpdateButtonClick(Sender: TObject);
[b]begin[/b]
  UpdateData;
[b]end[/b];

[b]procedure[/b] TForm1.Form1CloseQuery(Sender: TObject; [b]var[/b] CanClose: Boolean);
[b]begin[/b]
  [b]if[/b] Employees.Active [b]then[/b]
  [b]try[/b]
    [i]{ When closing, update the database if connected or save it to disk if not }[/i]
    [b]if[/b] Connection.Connected [b]then[/b]
      UpdateData [b]else[/b]
      SaveData;
  [b]except[/b]
    [b]on[/b] E: Exception [b]do[/b]
    [b]begin[/b]
      Application.HandleException(Self);
      CanClose := MessageDlg('Data not saved/updated, exit anyway?',
        mtConfirmation, mbYesNoCancel, 0) = mrYes;
    [b]end[/b];
  [b]end[/b];
[b]end[/b];

[b]procedure[/b] TForm1.ConnectionIndClick(Sender: TObject);
[b]begin[/b]
  [i]{ Toggle the connection's state }[/i]
  [b]if[/b] ConnectionInd.Checked [b]then[/b]
  [b]begin[/b]
    Connection.Open;
    Employees.Connection := Connection;
  [b]end[/b] [b]else[/b]
  [b]begin[/b]
    [i]{ Note here you must clear the connection property of the dataset before
      closing the connection.  Otherwise the dataset will close with the
      connection. }[/i]
    Employees.Connection := [b]nil[/b];
    Connection.Close;
  [b]end[/b];
[b]end[/b];

[b]procedure[/b] TForm1.RefreshButtonClick(Sender: TObject);
[b]begin[/b]
  [i]{ Close and reopen the dataset to refresh the data.  Note that in this demo
    there is no checking for pending updates so they are lost if you click
    the refresh data button before clicking the Update database button. }[/i]
  ConnectionInd.Checked := True;
  Employees.Close;
  Employees.CommandType := cmdTable;
  Employees.CommandText := 'Employee';
  Employees.Open;
[b]end[/b];

[b]end[/b].

bdominik schrieb am 18.10.2002 um 19:19 Uhr

oder ist das besser?

[b]unit[/b] BriefcaseMain;

[color="#000080"][i]{ This program demonstrates how to do disconnected briefcase applications
  with ADO.  When the Connected checkbox is unchecked the application is
  switched into offline mode.  If the application is exited at that point
  then the data is persisted to a file on disk (along with any edits to the
  data).  When the application is restarted it will load the persisted data
  if present, otherwise it will fetch the data from the database. }[/i][/color]

[b]interface[/b]

[b]uses[/b]
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, DB, ADODB, Grids, DBGrids, ExtCtrls;

[b]type[/b]
  TForm1 = [b]class[/b](TForm)
    Employees: TADODataSet;
    EmpSource: TDataSource;
    DBGrid1: TDBGrid;
    Connection: TADOConnection;
    Panel1: TPanel;
    ConnectionInd: TCheckBox;
    UpdateButton: TButton;
    RefreshButton: TButton;
    SaveButton: TButton;
    [b]procedure[/b] Form1Create(Sender: TObject);
    [b]procedure[/b] SaveButtonClick(Sender: TObject);
    [b]procedure[/b] Form1CloseQuery(Sender: TObject; [b]var[/b] CanClose: Boolean);
    [b]procedure[/b] UpdateButtonClick(Sender: TObject);
    [b]procedure[/b] ConnectionIndClick(Sender: TObject);
    [b]procedure[/b] RefreshButtonClick(Sender: TObject);
  [b]private[/b]
    DataFileName: [b]string[/b];
  [b]public[/b]
    [b]procedure[/b] LoadData;
    [b]procedure[/b] SaveData;
    [b]procedure[/b] UpdateData;
  [b]end[/b];

[b]var[/b]
  Form1: TForm1;

[b]implementation[/b]

[color="#000080"][i]{$R *.dfm}[/i][/color]

[b]const[/b]
  BaseFileName = 'EMPLOYEE.ADTG';

[b]procedure[/b] TForm1.LoadData;
[b]begin[/b]
  DataFileName := ExtractFilePath(Paramstr(0))+BaseFileName;
  [color="#000080"][i]{ If a persisted datafile exists, assume we exited in a disconnected
    (offline) state and load the data from the file. }[/i][/color]
  [b]if[/b] FileExists(DataFileName) [b]then[/b]
    Employees.LoadFromFile(DataFileName)
  [b]else[/b]
  [b]begin[/b]
    [color="#000080"][i]{ Otherwise establish the connection and get data from the database }[/i][/color]
    ConnectionInd.Checked := True;
    Employees.Open;
  [b]end[/b];
[b]end[/b];

[b]procedure[/b] TForm1.UpdateData;
[b]begin[/b]
  [color="#000080"][i]{ Connect to the database and send the pending updates }[/i][/color]
  ConnectionInd.Checked := True;
  Employees.UpdateBatch;
  DeleteFile(DataFileName);
[b]end[/b];

[b]procedure[/b] TForm1.SaveData;
[b]begin[/b]
  [color="#000080"][i]{ Persist the data to disk }[/i][/color]
  Employees.SaveToFile(DataFileName, pfADTG);
[b]end[/b];

[b]procedure[/b] TForm1.Form1Create(Sender: TObject);
[b]begin[/b]
  Connection.ConnectionString := 'FILE NAME=' + DataLinkDir + '\DBDEMOS.UDL';
  LoadData;
[b]end[/b];

[b]procedure[/b] TForm1.SaveButtonClick(Sender: TObject);
[b]begin[/b]
  SaveData;
[b]end[/b];

[b]procedure[/b] TForm1.UpdateButtonClick(Sender: TObject);
[b]begin[/b]
  UpdateData;
[b]end[/b];

[b]procedure[/b] TForm1.Form1CloseQuery(Sender: TObject; [b]var[/b] CanClose: Boolean);
[b]begin[/b]
  [b]if[/b] Employees.Active [b]then[/b]
  [b]try[/b]
    [color="#000080"][i]{ When closing, update the database if connected or save it to disk if not }[/i][/color]
    [b]if[/b] Connection.Connected [b]then[/b]
      UpdateData [b]else[/b]
      SaveData;
  [b]except[/b]
    [b]on[/b] E: Exception [b]do[/b]
    [b]begin[/b]
      Application.HandleException(Self);
      CanClose := MessageDlg('Data not saved/updated, exit anyway?',
        mtConfirmation, mbYesNoCancel, 0) = mrYes;
    [b]end[/b];
  [b]end[/b];
[b]end[/b];

[b]procedure[/b] TForm1.ConnectionIndClick(Sender: TObject);
[b]begin[/b]
  [color="#000080"][i]{ Toggle the connection's state }[/i][/color]
  [b]if[/b] ConnectionInd.Checked [b]then[/b]
  [b]begin[/b]
    Connection.Open;
    Employees.Connection := Connection;
  [b]end[/b] [b]else[/b]
  [b]begin[/b]
    [color="#000080"][i]{ Note here you must clear the connection property of the dataset before
      closing the connection.  Otherwise the dataset will close with the
      connection. }[/i][/color]
    Employees.Connection := [b]nil[/b];
    Connection.Close;
  [b]end[/b];
[b]end[/b];

[b]procedure[/b] TForm1.RefreshButtonClick(Sender: TObject);
[b]begin[/b]
  [color="#000080"][i]{ Close and reopen the dataset to refresh the data.  Note that in this demo
    there is no checking for pending updates so they are lost if you click
    the refresh data button before clicking the Update database button. }[/i][/color]
  ConnectionInd.Checked := True;
  Employees.Close;
  Employees.CommandType := cmdTable;
  Employees.CommandText := 'Employee';
  Employees.Open;
[b]end[/b];

[b]end[/b].

bdominik schrieb am 18.10.2002 um 19:26 Uhr

Klaro, unten ist besser!

KhanRKerensky schrieb am 18.10.2002 um 21:18 Uhr

Worum gehts?

bdominik schrieb am 18.10.2002 um 22:27 Uhr

habe vor 2 Jahren mal eine Code-Formater für PhpBB geschrieben, habe den wieder gefunden und ihn so umgeschrieben, dass er auch hier funzt. Er fomatiert Delphi quelltexte so, dass sie orndetlich aussehen!

Seite 1 von 1