怎样写个多线程进行数据库查询的程序呀?想了几天了:( 清高手指教谢谢!(14分)

  • 主题发起人 主题发起人 delphi3058
  • 开始时间 开始时间
D

delphi3058

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样写个多线程进行数据库查询的程序呀?想了几天了:( 清高手指教谢谢!
 
有想那个的时间还不如做优化查询语句+分页
 
多線程啊,多查些資料吧,這個一時也講不明白的。。
 
dbexpress + remotemodule
 
to hhjjhhjj:能具体讲讲吗?谢谢
 
我只是个提了个建议。我也不会写多线程数据库查询。
如果你做多线程的数据库查询的结果是用来显示数据的,不防改用分页的方法。
分页的讨论很多就是了。ADO提供的分页不怎么好,最好自己写代码来控制。
 
哈哈,真巧,我就在忙活这个,不过还要用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.
 
楼上代码我看过,其实一般程序能处理的话,最好不要用线程来处理,不一定好啊
 
将数据以olevariant返回,可以做到的
 
后退
顶部