如果要做线程就要做彻底!
如果能用线程查询数据当然好了!
由于你使用的是ADO,所以建议你可以在你的线程类里公布一个属性
TYourADOThread = class(TThread)
public
property QueryData: _Recordset read GetQueryData;
end;
GetQueryData的实现和简单: Result := ADOQuery1.Recordset;
///////////////////////
在主线程中,也创建一个TADODataSet,
在线程终止之前,将线程的QueryData赋值给ADODataSet的RecordSet属性。
下面是测试代码:
//////.pas
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls, Grids, DBGrids;
type
TADOThread = class(TThread)
private
FQuery: TADOQuery;
FConnection: TADOConnection;
function GetQueryData: _Recordset;
protected
procedure Execute;
override;
public
constructor Create(AConnStr, ASQL: string);
destructor Destroy;
override;
property QueryData: _Recordset read GetQueryData;
end;
TForm1 = class(TForm)
ADODataSet1: TADODataSet;
Button1: TButton;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
ADOConnection1: TADOConnection;
procedure Button1Click(Sender: TObject);
private
FThread: TADOThread;
proceduredo
OnThreadEnd(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
ActiveX;
{$R *.dfm}
{ TADOThread }
constructor TADOThread.Create(AConnStr, ASQL: string);
begin
inherited Create(True);
CoInitialize(nil);
FQuery := TADOQuery.Create(nil);
FConnection := TADOConnection.Create(nil);
FQuery.Connection := FConnection;
FConnection.ConnectionString := AConnStr;
FConnection.LoginPrompt := False;
FQuery.SQL.Text := ASQL;
end;
destructor TADOThread.Destroy;
begin
FQuery.Free;
FConnection.Free;
inherited;
end;
procedure TADOThread.Execute;
begin
FConnection.Connected := True;
FQuery.Open;
end;
function TADOThread.GetQueryData: _Recordset;
begin
Result := FQuery.Recordset;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
FThread := TADOThread.Create(ADOConnection1.ConnectionString, 'Select * from a');
FThread.OnTerminate :=do
OnThreadend;
FThread.Resume;
end;
procedure TForm1.DoOnThreadEnd(Sender: TObject);
begin
ADODataSet1.Recordset := TADOThread(Sender).QueryData;
end;
end.
////////////////////////////// .dfm
object Form1: TForm1
Left = 209
Top = 199
Width = 696
Height = 480
Caption = 'Form1'
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 Button1: TButton
Left = 240
Top = 64
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object DBGrid1: TDBGrid
Left = 40
Top = 120
Width = 505
Height = 209
DataSource = DataSource1
TabOrder = 1
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
end
object ADODataSet1: TADODataSet
Parameters = <>
Left = 168
Top = 64
end
object DataSource1: TDataSource
DataSet = ADODataSet1
Left = 136
Top = 64
end
object ADOConnection1: TADOConnection
ConnectionString =
'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:/data/Mydo
cument' +
's/db1.mdb;Persist Security Info=False'
Mode = cmShareDenyNone
Provider = 'Microsoft.Jet.OLEDB.4.0'
Left = 208
Top = 64
end
end