我找到出问题的地方了,主要是下面这段代码影响了速度,不知该如何优化?
在FORM1中有ADOCONNECTION1, ADOQUERY1, ADOQUERY2, DATASOURCE1, DBGRID1,
BUTTON1, BUTTON2其中ADOQUERY1, ADOQUERY2的CONNECTION属性均为ADOCONNECTION1
DATASOURCE1的DATASET为ADOQUERY1, datasource1的DATASOURCE属性为DATASOURCE1,
当单独按BUTTON1或BUTTON2时执行时间均正常大约不到1秒,但如果按了BUTTON2之后,
再去按BUTTON1会发现执行时间会需要3-4秒, 即按BUTTON2之后会使BUTTON1事件的运行
速度大大降低,我的记录数<800 以下为我的代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption:=DateTimeToStr(Now);
with ADOQuery1 do
begin
close;
SQL.clear;
sql.add('select * from customers');
open;
end;
Label2.Caption:=DateTimeToStr(Now);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Label1.Caption:=DateTimeToStr(Now);
with ADOQuery2 do
begin
Close;
SQL.Clear;
SQL.Add('select distinct contractno from customers');
Open;
first;
ComBoBox1.Items.Clear;
while not eof do
begin
ComBoBox1.Items.Add(ADOQuery2.Fields[0].AsString);
next;
end;
Label2.Caption:=DateTimeToStr(Now);
Close;
end;
end;