home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Runimage / Delphi50 / Demos / Ado / Briefcase / briefcasemain.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1999-08-11  |  3.9 KB  |  144 lines

  1. unit BriefcaseMain;
  2.  
  3. { This program demonstrates how to do disconnected briefcase applications
  4.   with ADO.  When the Connected checkbox is unchecked the application is
  5.   switched into offline mode.  If the application is exited at that point
  6.   then the data is persisted to a file on disk (along with any edits to the
  7.   data).  When the application is restarted it will load the persisted data
  8.   if present, otherwise it will fetch the data from the database. }
  9.  
  10. interface
  11.  
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   StdCtrls, Db, ADODB, Grids, DBGrids, ExtCtrls;
  15.  
  16. type
  17.   TForm1 = class(TForm)
  18.     Employees: TADODataSet;
  19.     EmpSource: TDataSource;
  20.     DBGrid1: TDBGrid;
  21.     Connection: TADOConnection;
  22.     Panel1: TPanel;
  23.     ConnectionInd: TCheckBox;
  24.     UpdateButton: TButton;
  25.     RefreshButton: TButton;
  26.     SaveButton: TButton;
  27.     procedure Form1Create(Sender: TObject);
  28.     procedure SaveButtonClick(Sender: TObject);
  29.     procedure Form1CloseQuery(Sender: TObject; var CanClose: Boolean);
  30.     procedure UpdateButtonClick(Sender: TObject);
  31.     procedure ConnectionIndClick(Sender: TObject);
  32.     procedure RefreshButtonClick(Sender: TObject);
  33.   private
  34.     DataFileName: string;
  35.   public
  36.     procedure LoadData;
  37.     procedure SaveData;
  38.     procedure UpdateData;
  39.   end;
  40.  
  41. var
  42.   Form1: TForm1;
  43.  
  44. implementation
  45.  
  46. {$R *.DFM}
  47.  
  48. const
  49.   BaseFileName = 'EMPLOYEE.ADTG';
  50.  
  51. procedure TForm1.LoadData;
  52. begin
  53.   DataFileName := ExtractFilePath(Paramstr(0))+BaseFileName;
  54.   { If a persisted datafile exists, assume we exited in a disconnected
  55.     (offline) state and load the data from the file. }
  56.   if FileExists(DataFileName) then
  57.     Employees.LoadFromFile(DataFileName)
  58.   else
  59.   begin
  60.     { Otherwise establish the connection and get data from the database }
  61.     ConnectionInd.Checked := True;
  62.     Employees.Open;
  63.   end;
  64. end;
  65.  
  66. procedure TForm1.UpdateData;
  67. begin
  68.   { Connect to the database and send the pending updates }
  69.   ConnectionInd.Checked := True;
  70.   Employees.UpdateBatch;
  71.   DeleteFile(DataFileName);
  72. end;
  73.  
  74. procedure TForm1.SaveData;
  75. begin
  76.   { Persist the data to disk }
  77.   Employees.SaveToFile(DataFileName, pfADTG);
  78. end;
  79.  
  80. procedure TForm1.Form1Create(Sender: TObject);
  81. begin
  82.   Connection.ConnectionString := 'FILE NAME=' + DataLinkDir + '\DBDEMOS.UDL';
  83.   LoadData;
  84. end;
  85.  
  86. procedure TForm1.SaveButtonClick(Sender: TObject);
  87. begin
  88.   SaveData;
  89. end;
  90.  
  91. procedure TForm1.UpdateButtonClick(Sender: TObject);
  92. begin
  93.   UpdateData;
  94. end;
  95.  
  96. procedure TForm1.Form1CloseQuery(Sender: TObject; var CanClose: Boolean);
  97. begin
  98.   if Employees.Active then
  99.   try
  100.     { When closing, update the database if connected or save it to disk if not }
  101.     if Connection.Connected then
  102.       UpdateData else
  103.       SaveData;
  104.   except
  105.     on E: Exception do
  106.     begin
  107.       Application.HandleException(Self);
  108.       CanClose := MessageDlg('Data not saved/updated, exit anyway?',
  109.         mtConfirmation, mbYesNoCancel, 0) = mrYes;
  110.     end;
  111.   end;
  112. end;
  113.  
  114. procedure TForm1.ConnectionIndClick(Sender: TObject);
  115. begin
  116.   { Toggle the connection's state }
  117.   if ConnectionInd.Checked then
  118.   begin
  119.     Connection.Open;
  120.     Employees.Connection := Connection;
  121.   end else
  122.   begin
  123.     { Note here you must clear the connection property of the dataset before
  124.       closing the connection.  Otherwise the dataset will close with the
  125.       connection. }
  126.     Employees.Connection := nil;
  127.     Connection.Close;
  128.   end;
  129. end;
  130.  
  131. procedure TForm1.RefreshButtonClick(Sender: TObject);
  132. begin
  133.   { Close and reopen the dataset to refresh the data.  Note that in this demo
  134.     there is no checking for pending updates so they are lost if you click
  135.     the refresh data button before clicking the Update database button. }
  136.   ConnectionInd.Checked := True;
  137.   Employees.Close;
  138.   Employees.CommandType := cmdTable;
  139.   Employees.CommandText := 'Employee';
  140.   Employees.Open;
  141. end;
  142.  
  143. end.
  144.