用Indy写了一个即时通讯的小软件,IdtcpServer创建的多线程操作数据库中,查了论坛里的资料了解到应该每一线程创建一个AdoConnectin连接通过AdoQuery来操作数据库,但是都没有实际的代码,应该怎么来写才行正确的创建和释放,有没有实际代码啊。谢谢了!

解决方案 »

  1.   

    就看你使用线程的数量有多少个了,以下仅供参考:
    //由主窗口传入一个TAdoDataset组件和ListView组件,若查询到结果就显示在主窗口的listview组件中.unit Unit2;interfaceuses
      Classes,DB,AdoDB,ComCtrls,SysUtils;type
      TQueryThread = class(TThread)
      private
        { Private declarations }
         FDataSet:TAdoDataset;
         FQueryException:Exception;
         ListView:TListview;
         procedure UpdateListView;
      protected
        procedure Execute; override;
      public
        constructor Create(Q:TAdoDataSet;LV:TListview); virtual;
      end;implementation
    uses
      windows,unit3;{ TQueryThread }constructor TQueryThread.Create(Q: TAdoDataSet;LV:TListview);
    begin
      inherited Create(true);
      ListView:=LV;
      FDataSet := Q;
      FDataset.CommandText :='SELECT * FROM TABLE'
      FreeOnTerminate:=true;
      Resume;
    end;
    procedure TQueryThread.Execute;
    begin
      try
        FDataset.Open;
        if not FdataSet.IsEmpty then
        begin
          synchronize(Updatelistview);
        end;
      except
        FqueryException:=ExceptObject as Exception;
        raise FqueryException.Create('查找过程出错');
      end; 
    end;procedure TQueryThread.UpdateListView;
    var
      listItem:TlistItem;
    begin
      with listview do
      begin
        while not Fdataset.eof do
        begin
          listitem:=items.add;
          listitem.Caption :='..... '
          listitem.SubItems.Add(Fdataset.fieldbyname('A').asstring);
          listitem.SubItems.Add(Fdataset.fieldbyname('Br').asstring);
         //.......
          Fdataset.next;
        end;
      end;
    end;end.  
      

  2.   

    可不可以设置全部变量关注Query是否正在执行,如果在执行的话此线程就等待,这个样子就浪费了多线程的效率,如果可以的话,等待一段时间之后应该怎么返回去再执行呢。