ADO多线程数据库查询
Posted tobetterlife
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ADO多线程数据库查询相关的知识,希望对你有一定的参考价值。
{ADO查询多线程单元} unit ADOThread; interface uses Classes,StdCtrls,ADODB; type TADOThread = class(TThread) private { Private declarations } FListBox:TListBox; FLabel:TLabel; ConnString:WideString; FSQLString:string; sl:TStrings; procedure UpdateCount; protected procedure Execute; override; public constructor Create(SQL:string;LB:TListBox;Lab:TLabel); end; implementation uses unit1,SysUtils,ActiveX; { TADOThread } constructor TADOThread.Create(SQL: string; LB: TListBox;Lab:TLabel); begin ConnString:=Form1.con1.ConnectionString; FListBox:=LB; FLabel:=Lab; FSQLString:=SQL; Inherited Create(False); end; procedure TADOThread.Execute; var Qry:TADOQuery; i:Integer; begin { Place thread code here } FreeOnTerminate:=True; sl:=TStringList.Create; CoInitialize(nil); //必须调用(需Uses ActiveX) Qry:=TADOQuery.Create(nil); try Qry.ConnectionString:=ConnString; //必须有自己的连接 Qry.Close; Qry.SQL.Clear; Qry.SQL.Add(FSQLString); Qry.Open; FListBox.Clear; for i := 1 to 1000 do //为了执行久点重复历遍数据集101次 begin Qry.First; while not Qry.Eof And not Terminated do begin //FListBox.AddItem(Qry.Fields[0].asstring,nil); //FListBox.Items.Add(Qry.Fields[0].asstring); sl.Add(Qry.Fields[0].asstring); //如果不调用Synchronize,会出现Canvas Does NOT Allow Drawing //Synchronize(UpdateCount); Qry.Next; end; //Qry.First; //FListBox.AddItem(‘*******‘,nil); //Synchronize(UpdateCount); end; Synchronize(UpdateCount); finally sl.Free; Qry.Free; end; CoUninitialize; end; procedure TADOThread.UpdateCount; begin //FLabel.Caption:=IntToStr(FListBox.Items.Count); FListBox.Items.Assign(sl); FLabel.Caption:=IntToStr(sl.Count); end; end. unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DB, ADODB, StdCtrls; type TForm1 = class(TForm) lbl1: TLabel; lbl2: TLabel; lbl3: TLabel; lst1: TListBox; lst2: TListBox; lst3: TListBox; cbb1: TComboBox; cbb2: TComboBox; cbb3: TComboBox; btn1: TButton; qry1: TADOQuery; con1: TADOConnection; procedure FormCreate(Sender: TObject); procedure btn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses ADOThread; procedure TForm1.FormCreate(Sender: TObject); var strSQL:string; begin strSQL:=‘select * from tb_student‘; qry1.Close; qry1.SQL.Clear; qry1.SQL.Add(strSQL); qry1.Open; cbb1.Clear; cbb2.Clear; cbb3.Clear; //将客户Company和相关CustNo填到ComboBox中 while not qry1.Eof do begin //cbb1.AddItem(qry1.Fields[1].asString, TObject(qry1.Fields[0].AsInteger)); cbb1.Items.Add(qry1.fieldbyname(‘smc‘).AsString); qry1.Next; end; cbb2.Items.Assign(cbb1.Items); cbb3.Items.Assign(cbb1.Items); // 默认选中第一个 cbb1.ItemIndex := 0; cbb2.ItemIndex := 0; cbb3.ItemIndex := 0; end; procedure TForm1.btn1Click(Sender: TObject); const SQL_CONST=‘select sxm from tb_student where sxm‘; var c1,c2,c3:Integer; s1,s2,s3:string; begin //取得三个选择框客户的编码 s1:=SQL_CONST+QuotedStr(cbb1.Text); s2:=SQL_CONST+QuotedStr(cbb2.Text); s3:=SQL_CONST+QuotedStr(cbb3.Text);//三个线程同时查询 TADOThread.Create(s1,lst1,lbl1); TADOThread.Create(s2,lst2,lbl2); TADOThread.Create(s3,lst3,lbl3); end; end.
以上是关于ADO多线程数据库查询的主要内容,如果未能解决你的问题,请参考以下文章