哈哈,真巧,我就在忙活这个,不过还要用Indy传递结果给客户端,让我给你个简单的例子吧,代码很短:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, Grids, DBGrids, StdCtrls;
type
TQueryThread1 = class(TThread)
private
FDataSet : TADOQuery;
FMemo: TMemo;
procedure getData;
protected
procedure Execute;
override;
public
constructor Create(ConnectStr:String;SQLStr:String;
DispMemo: TMemo);
end;
TQueryThread2 = class(TThread)
private
FDataSet : TADOQuery;
FMemo: TMemo;
procedure getData;
protected
procedure Execute;
override;
public
constructor Create(ConnectStr:String;SQLStr:String;DispMemo: TMemo;AADOConnection: TADOConnection);
end;
TForm1 = class(TForm)
ADOQuery1: TADOQuery;
Memo1: TMemo;
Button1: TButton;
Memo2: TMemo;
Button2: TButton;
ADOConnection1: TADOConnection;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TDoThread }
constructor TQueryThread1.Create(ConnectStr:String;SQLStr:String;DispMemo: TMemo);
begin
FDataSet := TADOQuery.Create(NIL);
FDataSet.ConnectionString := ConnectStr;
FDataSet.SQL.Text := SQLStr;
FMemo := DispMemo;
FreeOnTerminate := True;
inherited Create(False);
// Priority := tpLower;
end;
procedure TQueryThread1.Execute;
begin
FDataSet.Open;
FMemo.Clear;
FMemo.Lines.Add('开始时间'+FormatDateTime('YYYY-MM-DD hh:mm:ZZZ',Now));
FDataSet.First;
With FDataSetdo
While Not Eofdo
begin
FMemo.Lines.Add('RequestID:'+FieldByName('RequestID').ASString+'--SubSysID'+FieldByName('SubSysID').AsString);
Next;
end;
FDataSet.Close;
FreeAndNil(FDataSet);
Application.ProcessMessages;
FMemo.Lines.Add('结束时间'+FormatDateTime('YYYY-MM-DD hh:mm:ZZZ',Now));
// Synchronize(GetData);
end;
procedure TQueryThread1.GetData;
begin
FMemo.Clear;
FMemo.Lines.Add('开始时间'+FormatDateTime('YYYY-MM-DD hh:mm:ZZZ',Now));
FDataSet.First;
With FDataSetdo
While Not Eofdo
begin
FMemo.Lines.Add('RequestID:'+FieldByName('RequestID').ASString+'--SubSysID'+FieldByName('SubSysID').AsString);
Next;
end;
FDataSet.Close;
FreeAndNil(FDataSet);
Application.ProcessMessages;
FMemo.Lines.Add('结束时间'+FormatDateTime('YYYY-MM-DD hh:mm:ZZZ',Now));
end;
constructor TQueryThread2.Create(ConnectStr:String;SQLStr:String;DispMemo: TMemo;AADOConnection: TADOConnection);
begin
FDataSet := TADOQuery.Create(NIL);
FDataSet.Connection := AADOConnection;
// FDataSet.ConnectionString := ConnectStr;
FDataSet.SQL.Text := SQLStr;
FMemo := DispMemo;
FreeOnTerminate := True;
inherited Create(False);
// Priority := tpLower;
end;
procedure TQueryThread2.Execute;
begin
FDataSet.Open;
FMemo.Clear;
FMemo.Lines.Add('开始时间'+FormatDateTime('YYYY-MM-DD hh:mm:ZZZ',Now));
FDataSet.First;
{ With FDataSetdo
While Not Eofdo
begin
FMemo.Lines.Add('RequestID:'+FieldByName('RequestID').ASString+'--SubSysID'+FieldByName('SubSysID').AsString);
Next;
end;
FDataSet.Close;
FreeAndNil(FDataSet);
Application.ProcessMessages;
FMemo.Lines.Add('结束时间'+FormatDateTime('YYYY-MM-DD hh:mm:ZZZ',Now));
}
Synchronize(GetData);
end;
procedure TQueryThread2.GetData;
begin
FMemo.Clear;
FMemo.Lines.Add('开始时间'+FormatDateTime('YYYY-MM-DD hh:mm:ZZZ',Now));
FDataSet.First;
With FDataSetdo
While Not Eofdo
begin
FMemo.Lines.Add('RequestID:'+FieldByName('RequestID').ASString+'--SubSysID'+FieldByName('SubSysID').AsString);
Next;
end;
FDataSet.Close;
FreeAndNil(FDataSet);
Application.ProcessMessages;
FMemo.Lines.Add('结束时间'+FormatDateTime('YYYY-MM-DD hh:mm:ZZZ',Now));
end;
procedure TForm1.Button1Click(Sender: TObject);
var
tmpConnectStr : String;
tmpSQLStr : String;
begin
tmpConnectStr := 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDB;Data Source=.';
tmpSQLStr := 'select top 500 * from tab1';
TQueryThread1.Create(tmpConnectStr,tmpSQLStr, memo1);
tmpConnectStr := 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDB;Data Source=.';
tmpSQLStr := 'select top 500 * from tab1';
TQueryThread1.Create(tmpConnectStr,tmpSQLStr, memo2);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
tmpConnectStr : String;
tmpSQLStr : String;
begin
tmpConnectStr := 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDB;Data Source=.';
tmpSQLStr := 'select top 500 * from tab1';
TQueryThread2.Create(tmpConnectStr,tmpSQLStr, memo1,ADOConnection1);
tmpConnectStr := 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDB;Data Source=.';
tmpSQLStr := 'select top 500 * from tab1';
TQueryThread2.Create(tmpConnectStr,tmpSQLStr, memo2,ADOConnection1);
end;
end.