Code: Select all
program ProjectFetch;
uses
Forms,
UnitFetch in 'UnitFetch.pas' {Form2},
UnitThreadFetch in 'UnitThreadFetch.pas';
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm2, Form2);
Application.Run;
end.
Code: Select all
object Form2: TForm2
Left = 246
Top = 114
Width = 870
Height = 640
Caption = 'Form2'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Panel1: TPanel
Left = 0
Top = 0
Width = 862
Height = 41
Align = alTop
TabOrder = 0
object BitBtn1: TBitBtn
Left = 8
Top = 8
Width = 75
Height = 25
Caption = 'Start'
TabOrder = 0
OnClick = BitBtn1Click
end
end
object DBGrid1: TDBGrid
Left = 0
Top = 41
Width = 862
Height = 565
Align = alClient
DataSource = DataSource1
TabOrder = 1
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
end
object MyConnection1: TMyConnection
Database = 'mydactest'
Username = 'root'
Server = 'server'
Connected = True
LoginPrompt = False
Left = 288
Top = 8
end
object MyQuery1: TMyQuery
Connection = MyConnection1
SQL.Strings = (
'SELECT * FROM detail a, detail b, detail c'
'-- , detail d, detail e, detail f')
FetchAll = True
Left = 320
Top = 8
end
object DataSource1: TDataSource
Left = 352
Top = 8
end
end
Code: Select all
unit UnitFetch;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, MemDS, DBAccess, MyAccess, StdCtrls, Buttons, Grids,
DBGrids, ExtCtrls, UnitThreadFetch;
type
TForm2 = class(TForm)
MyConnection1: TMyConnection;
MyQuery1: TMyQuery;
Panel1: TPanel;
DBGrid1: TDBGrid;
BitBtn1: TBitBtn;
DataSource1: TDataSource;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
FMyQueryFetch: TMyQueryFetch;
procedure OnFetchComplete(Sender: TObject);
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.BitBtn1Click(Sender: TObject);
begin
Caption := 'You can NOT use MyConnection1 and MyQuery1 now';
BitBtn1.Enabled := False;
FMyQueryFetch := TMyQueryFetch.Create(MyQuery1);
FMyQueryFetch.OnTerminate := OnFetchComplete;
FMyQueryFetch.Resume;
end;
procedure TForm2.OnFetchComplete(Sender: TObject);
begin
FMyQueryFetch := nil;
Caption := 'You can use MyConnection1 and MyQuery1 now';
DataSource1.DataSet := MyQuery1;
end;
end.
Code: Select all
unit UnitThreadFetch;
interface
uses
Classes, DB;
type
TMyQueryFetch = class(TThread)
private
FDataSet: TDataSet;
protected
procedure Execute; override;
public
constructor Create(DataSet: TDataSet);
end;
implementation
{ TMyQueryFetch }
constructor TMyQueryFetch.Create(DataSet: TDataSet);
begin
inherited Create(True);
FDataSet := DataSet;
FreeOnTerminate := True;
end;
procedure TMyQueryFetch.Execute;
begin
FDataSet.Open;
end;
end.