Unit QueryThread
interface
uses
Classes,messages,ADODB,DB,SysUtils,Dialogs,ActiveX;
type
Tmythread = class(TThread)
private
Fquery:TAdoquery; //查询
FConnection:TAdoconnection; //连接
FDataSource: TDataSource; { 与查询组件相关的数据感知组件 }
procedure ConnectDataSource;{ 连接数据查询组件和数据感知组件的方法 }
protected
procedure Execute; override;
public
constructor Create(Suspended:Boolean;Query:TAdoQuery;DS:TDataSource); { 线程构造器 }
destructor Destroy;override;
end;
implementation
procedure Tmythread.ConnectDataSource;{ 连接数据查询组件和数据感知组件的方法 }
begin
FDataSource.dataset:=Fquery;
end;
constructor Tmythread.Create(Suspended:Boolean;Query:TAdoQuery;DS: TDataSource);
begin
CoInitialize( nil ); // 这个在 线程中是必需的
try
Fconnection:=TAdoconnection.create(nil);
Fquery:=Query;
FDataSource:=DS;
Fquery.connection:=Fconnection;
with Fconnection do
begin
LoginPrompt:=False;
KeepConnection:=True;
ConnectionString:='Provider=MSDASQL.1;Persist Security Info=False;Data Source=honesty';
end;
inherited Create(Suspended);
except
on E: Exception do
begin
showmessage('创建 线程时出错!错误是:'+ E.Message );
end;
end;
end;
destructor Tmythread.Destroy;
begin
Fconnection.free; //释放
CoUnInitialize;
inherited Destroy;
end;
procedure Tmythread.Execute;
begin
try
FreeOnTerminate := true;
FQuery.Open; { 打开查询 }
Synchronize(ConnectDataSource); { 线程同步 }
except
showmessage(' 线程异常!'); { 线程异常 }
end;
end;
end.
interface
uses
Classes,messages,ADODB,DB,SysUtils,Dialogs,ActiveX;
type
Tmythread = class(TThread)
private
Fquery:TAdoquery; //查询
FConnection:TAdoconnection; //连接
FDataSource: TDataSource; { 与查询组件相关的数据感知组件 }
procedure ConnectDataSource;{ 连接数据查询组件和数据感知组件的方法 }
protected
procedure Execute; override;
public
constructor Create(Suspended:Boolean;Query:TAdoQuery;DS:TDataSource); { 线程构造器 }
destructor Destroy;override;
end;
implementation
procedure Tmythread.ConnectDataSource;{ 连接数据查询组件和数据感知组件的方法 }
begin
FDataSource.dataset:=Fquery;
end;
constructor Tmythread.Create(Suspended:Boolean;Query:TAdoQuery;DS: TDataSource);
begin
CoInitialize( nil ); // 这个在 线程中是必需的
try
Fconnection:=TAdoconnection.create(nil);
Fquery:=Query;
FDataSource:=DS;
Fquery.connection:=Fconnection;
with Fconnection do
begin
LoginPrompt:=False;
KeepConnection:=True;
ConnectionString:='Provider=MSDASQL.1;Persist Security Info=False;Data Source=honesty';
end;
inherited Create(Suspended);
except
on E: Exception do
begin
showmessage('创建 线程时出错!错误是:'+ E.Message );
end;
end;
end;
destructor Tmythread.Destroy;
begin
Fconnection.free; //释放
CoUnInitialize;
inherited Destroy;
end;
procedure Tmythread.Execute;
begin
try
FreeOnTerminate := true;
FQuery.Open; { 打开查询 }
Synchronize(ConnectDataSource); { 线程同步 }
except
showmessage(' 线程异常!'); { 线程异常 }
end;
end;
end.