G
grasshoper
Unregistered / Unconfirmed
GUEST, unregistred user!
1,我是想做一个多线程的ADO的数据库查询程序,然后点了查询按钮后不影响程序的其他操作。可这个程序一点按钮开始查询线程就报错,什么runtime error 216之类的。希望能提供个无错的解决方法~~~
2,我想让线程查询完成后触发Onterminate来执行ondone事件,将查询结果写入listview1。但我在按钮事件里添加不了t1.OnTerminate:=Form1.ondone;
把ondone事件写进线程类的事件里然后想在create时定义Onterminate事件也是不行。
小弟初学delphi,大家多指教~~~~这是我的第一贴
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls, ComCtrls, ExtCtrls;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
Button1: TButton;
ListView1: TListView;
Memo1: TMemo;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure writetolv(listview:TListView;ado:TADOQuery);
procedure Timer1Timer(Sender: TObject);
procedure ondone;
private
public
{ Public declarations }
end;
type
TsqlThread = class(TThread)
private
adoquery:TADOQuery;
adocnn:TADOConnection;
sqlex:string;
lv:TListView;
protected
procedure Execute;override;
public
constructor Create(sql2:string;adoquery2:TADOQuery);
destructor Destroy;override;
end;
var
Form1: TForm1;
t1:TsqlThread;
implementation
uses ComObj;
{$R *.dfm}
constructor TsqlThread.Create(sql2:string;adoquery2:TADOQuery);
begin
adocnn:=TADOConnection.Create(nil);
adocnn.KeepConnection:=true;
adocnn.LoginPrompt:=false;
adocnn.ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=pmdb;Data Source=127.0.0.1';
adoquery:=adoquery2;
sqlex:=sql2;
inherited Create(false);
end;
destructor TsqlThread.Destroy;
begin
inherited Destroy;
end;
procedure TsqlThread.Execute;
begin
adoquery.Connection:=adocnn;
if adoquery.Active then
begin
try
adoquery.Close;
adoquery.Active:=false;
adoquery.SQL.Clear;
adoquery.SQL.Add(sqlex);
adoquery.Open;
adoquery.Active:=false;
except
on e:Exceptiondo
begin
ShowMessage(e.Message);
exit;
end;
end;
end else
exit;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
sql:string;
begin
sql:='select * from pmdb.dbo.all_zone';
t1.Create(sql,ADOQuery1);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
writetolv(ListView1,ADOQuery1);
Timer1.Enabled:=false;
end;
procedure TForm1.writetolv(listview:TListView;ado:TADOQuery);
var
i:integer;
begin
try
//生成列名
for i := 0 to ado.Fields.Count-1do
with listview.Columns.Adddo
begin
Caption := ado.Fields.DisplayLabel;
end;
//将记录写入行
ado.First;
while not ado.Eofdo
begin
with ListView.Items.Adddo
begin
Caption:=ado.Fields[0].AsString;
//第一列
for i := 1 to ado.Fields.Count - 1do
//第2列到最后一列
SubItems.Add(trim(ado.Fields.AsString));
end;
ado.Next;
end;
ado.Close;
ado.Active:=false;
except
ShowMessage('写入失败');
exit;
end;
end;
procedure TForm1.ondone;
begin
Timer1.Enabled:=true;
end;
end.
2,我想让线程查询完成后触发Onterminate来执行ondone事件,将查询结果写入listview1。但我在按钮事件里添加不了t1.OnTerminate:=Form1.ondone;
把ondone事件写进线程类的事件里然后想在create时定义Onterminate事件也是不行。
小弟初学delphi,大家多指教~~~~这是我的第一贴
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls, ComCtrls, ExtCtrls;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
Button1: TButton;
ListView1: TListView;
Memo1: TMemo;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure writetolv(listview:TListView;ado:TADOQuery);
procedure Timer1Timer(Sender: TObject);
procedure ondone;
private
public
{ Public declarations }
end;
type
TsqlThread = class(TThread)
private
adoquery:TADOQuery;
adocnn:TADOConnection;
sqlex:string;
lv:TListView;
protected
procedure Execute;override;
public
constructor Create(sql2:string;adoquery2:TADOQuery);
destructor Destroy;override;
end;
var
Form1: TForm1;
t1:TsqlThread;
implementation
uses ComObj;
{$R *.dfm}
constructor TsqlThread.Create(sql2:string;adoquery2:TADOQuery);
begin
adocnn:=TADOConnection.Create(nil);
adocnn.KeepConnection:=true;
adocnn.LoginPrompt:=false;
adocnn.ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=pmdb;Data Source=127.0.0.1';
adoquery:=adoquery2;
sqlex:=sql2;
inherited Create(false);
end;
destructor TsqlThread.Destroy;
begin
inherited Destroy;
end;
procedure TsqlThread.Execute;
begin
adoquery.Connection:=adocnn;
if adoquery.Active then
begin
try
adoquery.Close;
adoquery.Active:=false;
adoquery.SQL.Clear;
adoquery.SQL.Add(sqlex);
adoquery.Open;
adoquery.Active:=false;
except
on e:Exceptiondo
begin
ShowMessage(e.Message);
exit;
end;
end;
end else
exit;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
sql:string;
begin
sql:='select * from pmdb.dbo.all_zone';
t1.Create(sql,ADOQuery1);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
writetolv(ListView1,ADOQuery1);
Timer1.Enabled:=false;
end;
procedure TForm1.writetolv(listview:TListView;ado:TADOQuery);
var
i:integer;
begin
try
//生成列名
for i := 0 to ado.Fields.Count-1do
with listview.Columns.Adddo
begin
Caption := ado.Fields.DisplayLabel;
end;
//将记录写入行
ado.First;
while not ado.Eofdo
begin
with ListView.Items.Adddo
begin
Caption:=ado.Fields[0].AsString;
//第一列
for i := 1 to ado.Fields.Count - 1do
//第2列到最后一列
SubItems.Add(trim(ado.Fields.AsString));
end;
ado.Next;
end;
ado.Close;
ado.Active:=false;
except
ShowMessage('写入失败');
exit;
end;
end;
procedure TForm1.ondone;
begin
Timer1.Enabled:=true;
end;
end.