通用多线程查询
{*******************************************************}
{ }
{ 通用线程类 }
{ }
{ 版权所有 (C) 2008 咏南工作室(陈新光) }
{ }
{*******************************************************}
unit uThread;
interface
uses
Classes,SysUtils;
Type
Tfun = procedure;//巧妙使用过程类型
TFunThread =Class(TThread)
private
fun:Tfun;
protected
procedure Execute;override;
public
Constructor Create(Afun:Tfun);
Destructor Destroy;override;
end;
implementation
constructor TFunThread.Create(Afun: Tfun);
begin
inherited Create(true);
FreeOnTerminate :=true;
fun:=aFun;
Resume;
end;
destructor TFunThread.Destroy;
begin
//do something
end;
procedure TFunThread.Execute;
begin
fun;
end;
end.
{*******************************************************}
{ }
{ 数据工厂 }
{ }
{ 版权所有 (C) 2008 咏南工作室 }
{ }
{*******************************************************}
//工厂模式的简化版
unit uDataFactory;
interface
uses
SysUtils,ADODB,DB,Classes,uCommFunc,ActiveX;
function GetADODataSet(Owner:TComponent;ASQL:string):TDataSet;
function GetBDEDataSet(Owner:TComponent;ASQL:string):TDataSet;
implementation
function GetADODataSet(Owner:TComponent;ASQL:string):TDataSet;
var
Conn:TADOConnection;
DataSet:TADOQuery;
sFileName,sSection:string;
begin
CoInitialize(nil);
sFileName:=GetINIFile;
sSection:='conn db';
Conn:=TADOConnection.Create(Owner);
with Conn do
begin
LoginPrompt:=False;
Provider:='SQLOLEDB';
Properties['Data Source'].Value:=ReadIniFile(sFileName,sSection,'server','');
Properties['User ID'].Value:=ReadIniFile(sFileName,sSection,'userid','');
Properties['Password'].Value:=ReadIniFile(sFileName,sSection,'password','');
Properties['Initial Catalog'].Value:=ReadIniFile(sFileName,sSection,'database','');
open;
end;
dataset:=tadoquery.Create(owner);
dataset.Connection:=conn;
with dataset do
begin
Close;
SQL.Clear;
SQL.Text:=ASQL;
Open;
end;
Result:=DataSet;
CoUninitialize;
end;
function GetBDEDataSet(Owner:TComponent;ASQL:string):TDataSet;
begin
//do something;
end;
end.
{*******************************************************}
{ }
{ 客户端调用 }
{ }
{ 版权所有 (C) 2008 咏南工作室(陈新光) }
{ }
{*******************************************************}
unit uMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGridEh, DB,uThread,uDataFactory;
type
TForm1 = class(TForm)
edt1: TEdit;
btn1: TButton;
dbgrdh1: TDBGridEh;
btn2: TButton;
ds1: TDataSource;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
FDataset:TDataSet;
procedure LinkDataset;
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure GetDataset;//封装进多线程的方法
implementation
{$R *.dfm}
procedure GetDataset;
begin
Form1.FDataset:=GetADODataSet(Form1,'select * from zhiydoc');
end;
procedure TForm1.btn1Click(Sender: TObject);
var
m:TFunThread;
begin
m:=TFunThread.Create(GetDataset);
LinkDataset;
end;
procedure TForm1.LinkDataset;
begin
ds1.DataSet:=FDataset;
end;
end.
{ }
{ 通用线程类 }
{ }
{ 版权所有 (C) 2008 咏南工作室(陈新光) }
{ }
{*******************************************************}
unit uThread;
interface
uses
Classes,SysUtils;
Type
Tfun = procedure;//巧妙使用过程类型
TFunThread =Class(TThread)
private
fun:Tfun;
protected
procedure Execute;override;
public
Constructor Create(Afun:Tfun);
Destructor Destroy;override;
end;
implementation
constructor TFunThread.Create(Afun: Tfun);
begin
inherited Create(true);
FreeOnTerminate :=true;
fun:=aFun;
Resume;
end;
destructor TFunThread.Destroy;
begin
//do something
end;
procedure TFunThread.Execute;
begin
fun;
end;
end.
{*******************************************************}
{ }
{ 数据工厂 }
{ }
{ 版权所有 (C) 2008 咏南工作室 }
{ }
{*******************************************************}
//工厂模式的简化版
unit uDataFactory;
interface
uses
SysUtils,ADODB,DB,Classes,uCommFunc,ActiveX;
function GetADODataSet(Owner:TComponent;ASQL:string):TDataSet;
function GetBDEDataSet(Owner:TComponent;ASQL:string):TDataSet;
implementation
function GetADODataSet(Owner:TComponent;ASQL:string):TDataSet;
var
Conn:TADOConnection;
DataSet:TADOQuery;
sFileName,sSection:string;
begin
CoInitialize(nil);
sFileName:=GetINIFile;
sSection:='conn db';
Conn:=TADOConnection.Create(Owner);
with Conn do
begin
LoginPrompt:=False;
Provider:='SQLOLEDB';
Properties['Data Source'].Value:=ReadIniFile(sFileName,sSection,'server','');
Properties['User ID'].Value:=ReadIniFile(sFileName,sSection,'userid','');
Properties['Password'].Value:=ReadIniFile(sFileName,sSection,'password','');
Properties['Initial Catalog'].Value:=ReadIniFile(sFileName,sSection,'database','');
open;
end;
dataset:=tadoquery.Create(owner);
dataset.Connection:=conn;
with dataset do
begin
Close;
SQL.Clear;
SQL.Text:=ASQL;
Open;
end;
Result:=DataSet;
CoUninitialize;
end;
function GetBDEDataSet(Owner:TComponent;ASQL:string):TDataSet;
begin
//do something;
end;
end.
{*******************************************************}
{ }
{ 客户端调用 }
{ }
{ 版权所有 (C) 2008 咏南工作室(陈新光) }
{ }
{*******************************************************}
unit uMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGridEh, DB,uThread,uDataFactory;
type
TForm1 = class(TForm)
edt1: TEdit;
btn1: TButton;
dbgrdh1: TDBGridEh;
btn2: TButton;
ds1: TDataSource;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
FDataset:TDataSet;
procedure LinkDataset;
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure GetDataset;//封装进多线程的方法
implementation
{$R *.dfm}
procedure GetDataset;
begin
Form1.FDataset:=GetADODataSet(Form1,'select * from zhiydoc');
end;
procedure TForm1.btn1Click(Sender: TObject);
var
m:TFunThread;
begin
m:=TFunThread.Create(GetDataset);
LinkDataset;
end;
procedure TForm1.LinkDataset;
begin
ds1.DataSet:=FDataset;
end;
end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/archive/2008/01/24/2940910.html