UpdateForm.pas
上传用户:fh681027
上传日期:2022-07-23
资源大小:1959k
文件大小:3k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit UpdateForm;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5.   IBDatabase, Db, IBCustomDataSet, IBUpdateSQL, IBQuery, DBTables,
  6.   ExtCtrls, DBCtrls, Grids, DBGrids, DBActns, ActnList, ComCtrls, ToolWin;
  7. type
  8.   TForm1 = class(TForm)
  9.     EmpDS: TDataSource;
  10.     IBQuery1: TIBQuery;
  11.     IBUpdateSQL1: TIBUpdateSQL;
  12.     IBDatabase1: TIBDatabase;
  13.     IBTransaction1: TIBTransaction;
  14.     DBGrid1: TDBGrid;
  15.     IBDataSet1: TIBDataSet;
  16.     ActionList1: TActionList;
  17.     DataSetCancel1: TDataSetCancel;
  18.     DataSetDelete1: TDataSetDelete;
  19.     DataSetEdit1: TDataSetEdit;
  20.     DataSetFirst1: TDataSetFirst;
  21.     DataSetInsert1: TDataSetInsert;
  22.     DataSetLast1: TDataSetLast;
  23.     DataSetNext1: TDataSetNext;
  24.     DataSetPost1: TDataSetPost;
  25.     DataSetPrior1: TDataSetPrior;
  26.     DataSetRefresh1: TDataSetRefresh;
  27.     acCommit: TAction;
  28.     acRollback: TAction;
  29.     ToolBar1: TToolBar;
  30.     ToolButton1: TToolButton;
  31.     ToolButton2: TToolButton;
  32.     ToolButton3: TToolButton;
  33.     ToolButton4: TToolButton;
  34.     ToolButton5: TToolButton;
  35.     ToolButton6: TToolButton;
  36.     ToolButton7: TToolButton;
  37.     ToolButton8: TToolButton;
  38.     ToolButton10: TToolButton;
  39.     ToolButton11: TToolButton;
  40.     ToolButton12: TToolButton;
  41.     ToolButton9: TToolButton;
  42.     procedure FormCreate(Sender: TObject);
  43.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  44.     procedure ActionUpdateTransactions(Sender: TObject);
  45.     procedure acCommitExecute(Sender: TObject);
  46.     procedure acRollbackExecute(Sender: TObject);
  47.   private
  48.     { Private declarations }
  49.   public
  50.     { Public declarations }
  51.   end;
  52. var
  53.   Form1: TForm1;
  54. implementation
  55. {$R *.DFM}
  56. uses
  57.   Registry;
  58. procedure TForm1.FormCreate(Sender: TObject);
  59. var
  60.   Reg: TRegistry;
  61. begin
  62.   Reg := TRegistry.Create;
  63.   try
  64.     Reg.RootKey := HKEY_LOCAL_MACHINE;
  65.     // Borland Shared/Data
  66.    {Reg.OpenKey('SoftwareBorlandBorland SharedData', False);
  67.     IBDatabase1.DatabaseName := Reg.ReadString('Rootdir') + 'employee.gdb';}
  68.     // InterBase 6 Bin + Sample database path
  69.     Reg.OpenKey('SoftwareBorlandInterBaseCurrentVersion', False);
  70.     IBDatabase1.DatabaseName := Reg.ReadString('RootDirectory') +
  71.       'examplesdatabaseemployee.gdb';
  72.   finally
  73.     Reg.CloseKey;
  74.     Reg.Free;
  75.   end;
  76.   EmpDS.DataSet.Open;
  77. end;
  78. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  79. var
  80.   nCode: Word;
  81. begin
  82.   if IBTransaction1.InTransaction then
  83.   begin
  84.     nCode := MessageDlg ('Commit Transaction? (No to rollback)',
  85.       mtConfirmation, mbYesNoCancel, 0);
  86.     case nCode of
  87.       mrYes: IBTransaction1.Commit;
  88.       mrNo: IBTransaction1.Rollback;
  89.       mrCancel: Action := caNone; // don't close
  90.     end;
  91.   end;
  92. end;
  93. procedure TForm1.ActionUpdateTransactions(Sender: TObject);
  94. begin
  95.   acCommit.Enabled := IBTransaction1.InTransaction;
  96.   acRollback.Enabled := acCommit.Enabled;
  97. end;
  98. procedure TForm1.acCommitExecute(Sender: TObject);
  99. begin
  100.   IBTransaction1.CommitRetaining;
  101. end;
  102. procedure TForm1.acRollbackExecute(Sender: TObject);
  103. begin
  104.   // or: IBTransaction1.RollbackRetaining;
  105.   IBTransaction1.Rollback;
  106.   IBTransaction1.StartTransaction;
  107.   EmpDS.DataSet.Open;
  108. end;
  109. end.