验证ado.net数据库连接池和数据库连接字符串有关

有关ado.net数据库连接池的概念,可以看 https://blog.csdn.net/qq_37326058/article/details/88296751

文章目录

            • 1,文件结构如下所示:
            • 2,三个类的代码
            • 3,执行结果

1,文件结构如下所示:

我创建了两个数据库连接的类和一个主程序类,每个类中有一个对应的连接数据库的字符串和两个连接数据库的方法,具体结构见图二所示。

两个数据库连接的类,每个类有各自独立的数据库连接字符串(但是在主函数中设置两个字符串完全相同)和两个连接数据库的方法,那么若果执行程序,我会有几个数据库连接池呢?
验证ado.net数据库连接池和数据库连接字符串有关_第1张图片

验证ado.net数据库连接池和数据库连接字符串有关_第2张图片

2,三个类的代码

DatabaseConnect1类的代码如下:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataBaseConnectPool
{
   public class DatabaseConnect1
    {
        //创建连接字符串
      //public static  SqlConnectionStringBuilder ConnStr = new SqlConnectionStringBuilder();
        public static void ExecDatabaseConnect11(object sender, DataBaseConnectEventArgs e)
        {
            SqlConnection conn = null;
            Console.WriteLine("Connection11发送者为:----" + (string)sender);
            for (int i = 1; i <= 10; ++i)
            {
                conn = new SqlConnection(e.ConnStr.ConnectionString);
                try
                {
                    conn.Open();
                    Console.WriteLine("Connection11-{0} is linked", i);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\nConnection11-异常信息:\n{0}", ex.Message);
                    break;
                }
            }
            Console.ReadLine();
        }
        public static void ExecDatabaseConnect12(object sender, DataBaseConnectEventArgs e)
        {
            SqlConnection conn = null;
            Console.WriteLine("Connection12发送者为:----" + (string)sender);
            for (int i = 1; i <= 10; ++i)
            {
                conn = new SqlConnection(e.ConnStr.ConnectionString);
                try
                {
                    conn.Open();
                    Console.WriteLine("Connection12-{0} is linked", i);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\nConnection12-异常信息:\n{0}", ex.Message);
                    break;
                }
            }
            Console.ReadLine();
        }
    }
}

DatabaseConnect2类的代码如下:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataBaseConnectPool
{
    public class DatabaseConnect2
    {
        /// 
        /// 创建连接字符串
        /// 
       // public static SqlConnectionStringBuilder ConnStr = new SqlConnectionStringBuilder();
        public static void ExecDatabaseConnect21(object sender, DataBaseConnectEventArgs e)
        {
            SqlConnection conn = null;
            Console.WriteLine("Connection21发送者为:----" + (string)sender);
            for (int i = 1; i <= 10; ++i)
            {
                conn = new SqlConnection(e.ConnStr.ConnectionString);
                try
                {
                    conn.Open();
                    Console.WriteLine("Connection21-{0} is linked", i);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\nConnection21-异常信息:\n{0}", ex.Message);
                    break;
                }
            }
            Console.ReadLine();
        }
        public static void ExecDatabaseConnect22(object sender, DataBaseConnectEventArgs e)
        {
            SqlConnection conn = null;
            Console.WriteLine("Connection22发送者为:----" + (string)sender);
            for (int i = 1; i <= 10; ++i)
            {
                conn = new SqlConnection(e.ConnStr.ConnectionString);
                try
                {
                    conn.Open();
                    Console.WriteLine("Connection22-{0} is linked", i);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\nConnection22-异常信息:\n{0}", ex.Message);
                    break;
                }
            }
            Console.ReadLine();
        }
    }
}


主程序类代码如下:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DataBaseConnectPool
{
    /// 
    /// 自定义事件参数
    /// 
    public class DataBaseConnectEventArgs : EventArgs
    {
        public DataBaseConnectEventArgs():base()
        {
            _connStr = new SqlConnectionStringBuilder();
            ConnStr = new SqlConnectionStringBuilder();
        }
        private SqlConnectionStringBuilder _connStr;
        public SqlConnectionStringBuilder ConnStr
        {
            get { return _connStr; }
            set { _connStr = value; }
        }
    }
    public class DataBaseConnectModel
    {
        public DataBaseConnectEventArgs DbconnEventArgs1 = new DataBaseConnectEventArgs();
        public DataBaseConnectEventArgs DbconnEventArgs2 = new DataBaseConnectEventArgs();
    }
    class Program
    {/*我这边的命名规则就是
        方法体内的属性小写,
        在类中的,供外部访问的属性,PASCAL命名法,
        在类中的,供内部使用的,用camel命名法,并且前缀_
        命名空间,类名,方法名 ,Pascal命名法
        方法参数,用camel命名法
         并尽量让我们从变量名中看出变量类型,如string类型后缀Str,Double类型后缀Dbl等
         */
         /// 
         /// 自定义事件句柄
         /// 
         /// 
         /// 
        public delegate void DataBaseConnectEventHandler(object sender, DataBaseConnectEventArgs e);
        static DataBaseConnectModel _dataBaseConnectModel = new DataBaseConnectModel();
        static void InitializeConnStr()
        {
            //设置connectsql1的连接字符串
            _dataBaseConnectModel.DbconnEventArgs1.ConnStr.DataSource = @"DESKTOP-H6LD3FA\SQLEXPRESS";
            _dataBaseConnectModel.DbconnEventArgs1.ConnStr.InitialCatalog = "DB_Test1";
            _dataBaseConnectModel.DbconnEventArgs1.ConnStr.UserID = "sa";
            _dataBaseConnectModel.DbconnEventArgs1.ConnStr.Password = "123456";
            _dataBaseConnectModel.DbconnEventArgs1.ConnStr.Pooling = true; //开启连接池
            _dataBaseConnectModel.DbconnEventArgs1.ConnStr.MinPoolSize = 0; //设置最小连接数为0
            _dataBaseConnectModel.DbconnEventArgs1.ConnStr.MaxPoolSize = 20; //设置最大连接数为50             
            _dataBaseConnectModel.DbconnEventArgs1.ConnStr.ConnectTimeout = 10; //设置超时时间为10秒
            //设置connectsql2的连接字符串
            _dataBaseConnectModel.DbconnEventArgs2.ConnStr.DataSource = @"DESKTOP-H6LD3FA\SQLEXPRESS";
            _dataBaseConnectModel.DbconnEventArgs2.ConnStr.InitialCatalog = "DB_Test1";
            _dataBaseConnectModel.DbconnEventArgs2.ConnStr.UserID = "sa";
            _dataBaseConnectModel.DbconnEventArgs2.ConnStr.Password = "123456";
            _dataBaseConnectModel.DbconnEventArgs2.ConnStr.Pooling = true; //开启连接池
            _dataBaseConnectModel.DbconnEventArgs2.ConnStr.MinPoolSize = 0; //设置最小连接数为0
            _dataBaseConnectModel.DbconnEventArgs2.ConnStr.MaxPoolSize = 20; //设置最大连接数为50 
            _dataBaseConnectModel.DbconnEventArgs2.ConnStr.ConnectTimeout = 10; //设置超时时间为10秒
        }
        static void Main(string[] args)
        {
            InitializeConnStr();
            //Thread t1 = new Thread(new ThreadStart(connectsql1.executeConnect1));
            //Thread t2 = new Thread(new ThreadStart(connectsql2.executeConnect1));
            //t1.Start(); t2.Start();
            DataBaseConnectEventHandler d1 = new DataBaseConnectEventHandler(DatabaseConnect1.ExecDatabaseConnect11);
            DataBaseConnectEventHandler d2 = new DataBaseConnectEventHandler(DatabaseConnect1.ExecDatabaseConnect12);
            DataBaseConnectEventHandler d3 = DatabaseConnect2.ExecDatabaseConnect21;
            DataBaseConnectEventHandler d4 = DatabaseConnect2.ExecDatabaseConnect22;
            object senderObj = "Main方法";
            IAsyncResult result1 = d1.BeginInvoke(senderObj, _dataBaseConnectModel.DbconnEventArgs1, null, null);
            IAsyncResult result2 = d2.BeginInvoke(senderObj, _dataBaseConnectModel.DbconnEventArgs1, null, null);
            IAsyncResult result3 = d3.BeginInvoke(senderObj, _dataBaseConnectModel.DbconnEventArgs2, null, null);
            IAsyncResult result4 = d4.BeginInvoke(senderObj, _dataBaseConnectModel.DbconnEventArgs2, null, null);
            d1.EndInvoke(result1);
            d2.EndInvoke(result2);
            d3.EndInvoke(result3);
            d4.EndInvoke(result4);
            Console.ReadLine();
            Console.ReadLine();
            Console.ReadLine();
        }
    }
}

3,执行结果

执行结果如下图所示,可以看出数据库连接池 和有多少个数据库连接类,或者有多少个数据库连接执行方法都没有关系,只要连接字符串相同,那么在一个程序集中大概就只有一个数据库连接池。也就可以大胆得出结论, 一个程序集中 ,数据库连接池的数量和数据库连接字符串是有关系的。
验证ado.net数据库连接池和数据库连接字符串有关_第3张图片

你可能感兴趣的:(验证ado.net数据库连接池和数据库连接字符串有关)