工厂模式之数据工厂

;数据库连接配置文件db.ini

[current database]
dbtype=access

[sql server]
database=wwater
userid=sa
password=
server=192.168.0.177

[access]
server=./db1.mdb
userid=admin
password=
dbpass=

[Oracle]
server=
userid=
password=

unit uADOFactory;

interface

uses
  SysUtils,ADODB,DB,IniFiles,Forms,Classes,ActiveX;

const
  c_SqlSvrPrd='sqloledb';
  c_AccessPrd='microsoft.jet.oledb.4.0';
  c_oracle='MSDAORA.1';

{ 生成TADOConnection }
function GetADOConn(Owner:TComponent):TADOConnection;
{ 生成TADOQuery }
function GetADOQuery(Owner:TComponent;AConn:TADOConnection;ASQL:string):TADOQuery;
{ 生成TDataSource对象 }
function GetDataSource(Owner:TComponent):TDataSource;
{ TDataSource绑定数据集 }
procedure LinkControl(ADataSet:TDataSet;ADataSource:TDataSource);

implementation

function GetDataSource(Owner:TComponent):TDataSource;
var
  FDataSource:TDataSource;
begin
  FDataSource:=TDataSource.Create(Owner);
  Result:=FDataSource;
end; 

procedure LinkControl(ADataSet:TDataSet;ADataSource:TDataSource);
begin
  ADataSource.DataSet:=ADataSet;
end;

function GetADOConn(Owner:TComponent):TADOConnection;
var
  Conn:TADOConnection;
  sFileName,sSection,sType:string;
  ini:TIniFile;
begin
  CoInitialize(nil);
  sFileName:=ExtractFilePath(Application.ExeName)+'db.ini';
  Conn:=TADOConnection.Create(Owner);
  with Conn do
  begin
    LoginPrompt:=False;
    ini:=TIniFile.Create(sFileName);
    sType:=ini.ReadString('current database','dbtype','');
    try
      if sType='sql server' then
      begin
        Provider:=c_SqlSvrPrd;
        sSection:='sql server';
        Properties['Data Source'].Value:=ini.ReadString(sSection,'server','');
        Properties['User ID'].Value:=ini.ReadString(sSection,'userid','');
        Properties['Password'].Value:=ini.ReadString(sSection,'password','');
        Properties['Initial Catalog'].Value:=
          ini.ReadString(sSection,'database','');
      end;
      if sType='access' then
      begin
        Provider:=c_AccessPrd;
        sSection:='access';
        Properties['Jet OLEDB:Database Password'].Value:=
          ini.ReadString(sSection,'database password','');
        Properties['Data Source'].Value:=ini.ReadString(sSection,'server','');
        Properties['User ID'].Value:=ini.ReadString(sSection,'userid','');
        Properties['Password'].Value:=ini.ReadString(sSection,'password','');
      end;
      if sType='oracle' then
      begin
        Provider:=c_oracle;
        sSection:='oracle';
        Properties['Data Source'].Value:=ini.ReadString(ssection,'server','');
        Properties['User ID'].Value:=ini.ReadString(sSection,'userid','');
        Properties['Password'].Value:=ini.ReadString(sSection,'password','');
      end;
      try
        Connected:=True;
        Result:=Conn;
      except
        Result:=nil;
        raise Exception.Create('数据库连接失败');
      end;
    finally
      ini.Free;
    end;
  end;
  CoUninitialize;
end;

function GetADOQuery(Owner:TComponent;AConn:TADOConnection;ASQL:string):TADOQuery;
var
  DataSet:TADOQuery;
begin
  dataset:=TADOQuery.Create(owner);
  dataset.Connection:=aconn;
  with dataset do
  begin
    Close;
    SQL.Clear;
    SQL.Text:=ASQL;
    Open;
  end;
  Result:=DataSet;
end;

end.

你可能感兴趣的:(工厂模式)