zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    ABP 基于DDD的.NET开发框架 学习(四)时间控件采用datetimepicker注意事项
    解决Vs控制台程序出现NuGetprofile.ps1,因为在此系统上禁止运行脚本错误时或提示:“无法加载文件 .nugetpackagesMicrosoft.EntityFrameworkCore.Tools1.1.0-preview4-final oolsinit.ps1,因为在此系统上禁止运行脚本”
    git 本地初始化项目操作
    php(thinkphp)在linux系统下pdf转png图片【转】
    git学习笔记
    git 2.4.5编译安装
    mysql占用内存高的问题
    vmware下虚拟机centos,root登录时候提示鉴定故障解决方法
    SSH.net之主程序
    SSH.net之Service层
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2940814.html
Copyright © 2011-2022 走看看