zoukankan      html  css  js  c++  java
  • 开发一个c#的数据库连接池

    c#操作数据库是一个经典,用习惯了以后真感觉不错,很简单的。现在很多关系数据库都支持c#。c#的ADO.NET规范都遵守。 对于一般的设置,ADO.NET都放在数据库连接字符串上。比如池化,连接超时等。 所以C#的连接池一般需要数据库开发的客户端直接支持池化,是由数据库客户端驱动提供的,如果没有实现ADO.NET池化规范则,就没有池,其实 我都很少看见。   不说了,根据ADO.NET规范,我开发了一个简单的池,主要就是配置和驱动连接的保持,用linkedlist实现数据保持。用法和普通连接对象使用一样。 项目取名称:Hikari,默认驱动dll放在DBDrivers文件夹下面。 基本使用:  

    HikariConfig hikariConfig = new HikariConfig();             
    hikariConfig.DBType = "SqlServer";           
      hikariConfig.ConnectString = "Server = 127.0.0.1; Port = 5432; User Id = postgres; Password = 1234; Database = postgres;Pooling=true; ";             //hikariConfig.DriverDir = "DBDrivers";          
       //hikariConfig.DriverDLL = "XXXX.dll";         
        //hikariConfig.DBTypeXml = "DBType.xml";       
          HikariDataSource hikariDataSource = new HikariDataSource(hikariConfig); 
          var connection = hikariDataSource.GetConnection();                  
       if (connection != null)  {    
     var cmd = connection.CreateCommand();                       
      cmd.CommandText = "select * from student";              
               var rd = cmd.ExecuteReader();                  
           int datanum = 0;                     
        while (rd.Read())                     
        {                            
     rd.GetInt32(0);       
       rd.GetString(1);                          
       rd.GetInt32(2);                         
        datanum++;                         
    }                        
     rd.Close();                        
     cmd.Dispose();                       
      connection.Close();

    项目已经上传CSDN,同时我的GIT也已经上传,发现bug或者优化会更新。 项目地址: https://github.com/jinyuttt/Hikari.git

  • 相关阅读:
    统计代码测试覆盖率-Python
    第一篇
    svn统计代码行数(增量)
    android多渠道打包
    解决Error:All flavors must now belong to a named flavor dimension. Learn more at...
    Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{...
    解决Android编译时出现aapt.exe finished with non-zero exit value 1
    自定义Json解析工具
    Process 'command 'D:jdk8jdkinjava.exe'' finished with non-zero exit value 2
    C:Program FilesJavajdk1.7.0_79injava.exe'' finished with non-zero exit value 1
  • 原文地址:https://www.cnblogs.com/jinyu20180311/p/10312354.html
Copyright © 2011-2022 走看看