80
最 新 评 论
80
专 题 分 类
80
最 新 日 志
80
最 新 留 言
80
搜 索
用 户 登 录
友 情 连 接
我 的 好 友
80
博 客 信 息
80


Delphi 采用线程处理数据库样例 | 2012/8/5 23:40:00
第一步:
IdThreadComponent
 
Procedure ExecSQLcommandGetResult(DBConnectStr:String;DBSQLCommand:Tstrings;DBShow:TStringGrid);
第二步:
type
  TThreadGetDBTIX = class(TThread)
  private
 
  protected
      procedure Execute; override;
  public
  end;
第三步:
procedure TThreadGetDBTIX.Execute();
begin
     FreeOnTerminate:=true;
     CoInitialize(nil);
     try
        ExecSQLcommandGetResult(GetDBConnectString,GetSQLCommand,MainForm.NetWorkParaShowStringGrid);
     except
        GetSQLCommand.Free;
     end;
end;
第四步:
{=======================================================================================
本程序段说明:  获取数据库表中数据
========================================================================================}
Procedure ExecSQLcommandGetResult(DBConnectStr:String;DBSQLCommand:Tstrings;DBShow:TStringGrid);
var
          i,j:longint;
          ADOQuery:TADOQuery;
          DataSource:TDataSource;
begin
          ADOQuery:=TADOQuery.Create(nil);
          DataSource:=TDataSource.Create(nil);

          DataSource.DataSet:=ADOQuery;

          ADOQuery.ConnectionString:=DBConnectStr;
          ADOQuery.sql.clear;
          ADOQuery.sql.AddStrings(DBSQLCommand);
          ADOQuery.Active:=True;

          DBShow.ColCount:=ADOQuery.fielddefs.Count;
          DBShow.RowCount:=DataSource.DataSet.RecordCount+1;

          for i:=0 to ADOQuery.fielddefs.Count-1 do
          begin
              Application.ProcessMessages;
              DBShow.Cells[i,0]:=ADOQuery.fielddefs[i].Name;
              DBShow.ColWidths[i]:=12*length(trim(MainForm.NetWorkParaShowStringGrid.Cells[i,0]));
          end;

          j:=0;
          ADOQuery.First;
          while not ADOQuery.eof do
          begin
              j:=j+1;
              Application.ProcessMessages;
              for i:=0 to DataSource.DataSet.fielddefs.Count-1 do
              begin
                  Application.ProcessMessages;
                  DBShow.Cells[i,j]:=ADOQuery.Fields.Fields[i].AsString;
              end;
              ADOQuery.Next;
              MainForm.RzStatusPane.Caption:=inttostr(j-1);
          end;

          if Assigned(ADOQuery) then ADOQuery.free;
          if Assigned(DataSource) then DataSource.free;
end;
阅读全文 | 回复(0) | 引用通告 | 编辑 | By: 皮皮爸爸
  • 标签:Delphi 
  • 发表评论:

      大名:
      密码: (游客无须输入密码)
      主页:
      标题:
      80
    Powered by Oblog.