• ado连接池


    //==============================================================================
    // ADO连接池         cxg                  2008-09-26 16:47:40
    //==============================================================================

    unit uADOPool;

    interface

    uses
      SysUtils,Classes,ADODB,Dialogs,IniFiles,Forms;

    type// 数据库类型
      TDBType=(Access,SqlServer,Oracle);

    type// 记录每个TADOConnection对象的状态
      Tado=record
        FName:string;
        FUsed:Boolean;             // 此TADOConnection对象是否已被使用
        P:TADOConnection;
      end;

    // 动态生成n个TADOConnection并保存进对象池中
    procedure ADOPool(Owner:TComponent;DBType:TDBType;MaxNums:Integer);
    // 从连接池里取空闲TADOConnection
    function GetADOConnection:TADOConnection;
    // 使用完后使TADOConnection空闲出来
    procedure SetNonUsed(AdoConnection:TADOConnection);

    var
      ADOArray:array of Tado;        // 连接池容器

    implementation

    // 动态生成N个TADOConnection并保存进对象池中
    // MaxNums 最大的连接对象数量
    procedure ADOPool(Owner:TComponent;DBType:TDBType;MaxNums:Integer);
    var
      i:Integer;
      Conn:TADOConnection;
      sFileName,sSection:string;
      ini:TIniFile;
    begin
      sFileName:=ExtractFilePath(Application.ExeName)+'db.ini';
      SetLength(ADOArray,MaxNums);
     
      for i:=Low(ADOArray) to High(ADOArray) do
      begin
        with ADOArray[i] do
        begin
          FName:='con'+inttostr(i);
          FUsed:=False;

          Conn:=TADOConnection.Create(Owner);
          with Conn do
          begin
            p:=Conn;
            Conn.Name:=FName;
            LoginPrompt:=False;
            ini:=TIniFile.Create(sFileName);
            try
              case DBType of
                sqlserver:
                begin
                  sSection:='sql server';
                  Provider:=ini.ReadString(sSection,'provider','');
                  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;

                access:               
                begin
                  sSection:='access';
                  Provider:=ini.ReadString(sSection,'provider','');
                  Properties['Jet OLEDB:Database Password'].Value:=ini.ReadString(sSection,'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;

                oracle:
                begin
                  sSection:='oracle';
                  Provider:=ini.ReadString(sSection,'provider','');
                  Properties['Data Source'].Value:=ini.ReadString(sSection,'server','');
                  Properties['User ID'].Value:=ini.ReadString(sSection,'userid','');
                  Properties['Password'].Value:=ini.ReadString(sSection,'password','');
                end;
              end;
             
              try
                Connected:=True;
              except
                raise Exception.Create('数据库连接失败');
              end;
            finally
              ini.Free;
            end;
          end;
        end;
      end;
    end;

    function GetADOConnection:TADOConnection;
    var
      i,t:Integer;
    begin
      t:=0;
      for i:=Low(ADOArray) to High(ADOArray) do
      begin
        if not ADOArray[i].FUsed then
        begin
          ADOArray[i].FUsed:=True;
          Result:=ADOArray[i].P;
          Inc(t);
          Break;
        end;
      end;
      if t=0 then ShowMessage('没有空闲的连接');
    end;

    procedure SetNonUsed(AdoConnection:TADOConnection);
    var
      i:Integer;
    begin
      for i:=Low(ADOArray) to High(ADOArray) do
      begin
        if ADOArray[i].FName=AdoConnection.Name then
        begin
          ADOArray[i].FUsed:=False;
          Break;
        end; 
      end; 
    end;  

    end.

  • 相关阅读:
    Invalid call to dataType on unresolved object, tree: 'goodsid的问题
    本地调试spark程序出现Please set spark.sql.hive.metastore.jars 一类配置错误的问题
    es搜索模型例子
    mysql2es全量更新方案
    利用hive-hbase表做hive表快速入库hbase功能,纬度表的查询
    运行spark报错Error while instantiating 'org.apache.spark.sql.hive.HiveSessionState'
    es常用查询学习记录
    SPSS聚类与判别
    Matlab常微分方程数值解法(1)
    Matlab拟合
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2940814.html
Copyright © 2020-2023  润新知