ADOマルチスレッドデータベースクエリー

11579 ワード

{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.