zoukankan      html  css  js  c++  java
  • 工厂模式之数据工厂

    ;数据库连接配置文件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.

  • 相关阅读:
    【MFC】在CHtmlView中在同一窗口显示新打开页面
    【MFC】CHtmlView::GetSource中文乱码的问题
    【Win32】对指定进程进行禁音
    【MFC】在CHtmlView中准确判断页面加载完成
    【MFC】CHtmlView或WebBrowser禁止脚本错误提示
    【MFC】CDialogBar中按钮不可用
    【转载记录】Accessing Device Drivers from C#
    【源代码R3】移植一份LARGE_INTEGER转时间的代码
    Square Root of Permutation
    MySQL创建视图命令
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2940906.html
Copyright © 2011-2022 走看看