前篇回顾:应用程序池在项目中引发的问题不多,不过如果遇到了没有经历过的话可能会比较茫然,呵呵。当然重点还是数据库连接池与线程池了。
本篇虫子就结合demo程序给大家演示下数据库连接池会给大家的程序带来什么样的影响。
private string mConnectionString = "server=127.0.0.1;uid=chongzi;pwd=123456;database=AntiXSS;Connection Reset=FALSE;Max Pool Size=15" |
连接字符串中设定池化,池的最大数目为15
[Serializable] public class DAL : IDisposable { public DAL() { DbHelper dh = new DbHelper(); DataSet ds = dh.excSqlRetDataSet("select getdate()"); Console.WriteLine(ds.Tables[0].Rows[0][0].ToString()); } public void Dispose() { } } |
public DataSet excSqlRetDataSet(string sqlString) { DataSet ds = null; if (sqlString.Length == 0) { return ds; } for (int i = 0; i < 20; i++) { SqlConnection conn = new SqlConnection(mConnectionString); ds = new DataSet(); string sqlCommand = sqlString; SqlDataAdapter da = new SqlDataAdapter(sqlCommand, conn); da.Fill(ds); //注意了 我这里没有关闭链接 //conn.Close(); } return ds; } |
初始数据库连接数1
启动控制台应用程序
开启3个进程
4个!!! 很好 至于为什么不是61个 虫子先卖个关子留到下面再说