Professional Documents
Culture Documents
Performing Database Queries in A Background Thread
Performing Database Queries in A Background Thread
thread
Written by Embarcadero USA on Monday, 29 March 1999 Posted in DATA
Description:
This document explains how to perform queries in a
background thread by using the TThread class. For
information on the general usage of the TThread class,
please refer to the Borland documentation and to the online
help. You should be aware of how to use Delphi's
database components to understand the this TI's contents.
Session1
Active True;
SessionName "Ses1"
DataBase1
AliasName "IBLOCAL"
DatabaseName "DB1"
SessionName "Ses1"
Query1
DataBaseName "DB1"
SessionName "Ses1"
SQL.Strings "Select * from employee"
DataSource1
DataSet ""
DBGrid1
DataSource DataSource1
Session2
Active True;
SessionName "Ses2"
DataBase2
AliasName "IBLOCAL"
DatabaseName "DB2"
SessionName "Ses2"
Query2
DataBaseName "DB2"
SessionName "Ses2"
SQL.Strings "Select * from customer"
DataSource2
DataSet ""
DBGrid1
DataSource DataSource2
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs,
StdCtrls, Grids, DBGrids, DB, DBTables;
type
TForm1 = class(TForm)
Session1: TSession;
Session2: TSession;
Database1: TDatabase;
Database2: TDatabase;
Query1: TQuery;
Query2: TQuery;
DataSource1: TDataSource;
DataSource2: TDataSource;
DBGrid1: TDBGrid;
DBGrid2: TDBGrid;
GoBtn1: TButton;
procedure GoBtn1Click(Sender: TObject);
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
Form1: TForm1;
implementation
procedure TQueryThread.Execute;
begin
try
{ Run the query and connect the datasource to the TQuery
component by calling ConnectDataSource from main
thread (Synchronize used for this purpose)}
FQuery.Open;
Synchronize(ConnectDataSource);
except
{ Capture exception, if one occurs, and handle it in the
context of the main thread (Synchonize used for this
purpose. }
FQueryException := ExceptObject as Exception;
Synchronize(ShowQryError);
end;
end;
procedure TQueryThread.ConnectDataSource;
begin
FDataSource.DataSet := FQuery; // Connect the DataSource
to the TQuery
end;
procedure TQueryThread.ShowQryError;
begin
Application.ShowException(FQueryException); // Handle the
exception
end;
{$R *.DFM}
end.
Reference:
3/30/99 11:18:31 AM
No iframes
No iframes
No iframes
No iframes
No iframes
home
company
communities
partners
copyright
privacy
report software piracy