Mainfrm.PAS
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:2k
- unit Mainfrm;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, DB, DBTables, Buttons, Grids, DBGrids;
- type
- TfrmMain = class(TForm)
- Database1: TDatabase;
- Query1: TQuery;
- DataSource1: TDataSource;
- Database2: TDatabase;
- Query2: TQuery;
- DataSource2: TDataSource;
- btnQuery: TSpeedButton;
- DBGrid1: TDBGrid;
- DBGrid2: TDBGrid;
- Session2: TSession;
- Session1: TSession;
- procedure btnQueryClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- //后台查询数据库的线程类
- TQueryThread = class(TThread)
- private
- FSession: TSession;
- FDatabase: TDataBase;
- FQuery: TQuery;
- FDatasource: TDatasource;
- FQueryException: Exception;
- procedure ConnectDataSource;
- procedure ShowQryError;
- protected
- procedure Execute; override;
- public
- constructor Create(Session: TSession; DataBase:
- TDatabase; Query: TQuery; DataSource: TDataSource);virtual;
- end;
- var
- frmMain: TfrmMain;
- implementation
- {$R *.dfm}
- //TQueryThread的实现
- constructor TQueryThread.Create(Session: TSession; DataBase:
- TDatabase; Query: TQuery; Datasource: TDataSource);
- begin
- inherited Create(False); //以立即执行方式创建线程
- //将传进来的数据库的相关对象赋给私有变量
- FSession := Session;
- FDatabase := DataBase;
- FQuery := Query;
- FDataSource := Datasource;
- FreeOnTerminate := True; // //当线程执行完,终止线程
- end;
- procedure TQueryThread.Execute;
- begin
- try
- FQuery.Open;
- Synchronize(ConnectDataSource);
- except
- FQueryException := ExceptObject as Exception;
- Synchronize(ShowQryError);
- end;
- end;
- procedure TQueryThread.ConnectDataSource;
- begin
- FDataSource.DataSet := FQuery; //连接DataSource到数据集Query上
- end;
- procedure TQueryThread.ShowQryError;
- begin
- Application.ShowException(FQueryException);
- end;
- procedure RunBackgroundQuery(Session: TSession; DataBase:
- TDataBase;Query: TQuery; DataSource:TDataSource);
- begin
- TQueryThread.Create(Session, Database, Query, DataSource);
- end;
- //TfrmMain实现
- procedure TfrmMain.btnQueryClick(Sender: TObject);
- begin
- RunBackgroundQuery(Session1, DataBase1, Query1,Datasource1);
- RunBackgroundQuery(Session2, DataBase2, Query2,Datasource2);
- end;
- end.