Connection Reset 'true' 确定在从池中移除数据库连接时是否将其重置。对于 Microsoft SQL Server 版本 7.0,如果设置为 false,将避免在获取连接时经历一个额外的往返过程,但必须注意的是连接状态(如数据库上下文)不会被重置。 Enlist 'true' 当为 true 时,如果存在事务上下文,池管理程序将自动在创建线程的当前事务上下文中登记连接。 Max Pool Size 100 池中允许的最大连接数。 Min Pool Size 0 池中维护的最小连接数。 Pooling 'true' 当为 true 时,将从相应的池中取出连接,或者在必要时创建连接并将其添加到相应的池中。
实例代码: namespace HowTo.Samples.ADONET {
using System; using System.Data.SqlClient;
public class connectionpooling { public static void Main() { connectionpooling myconnectionpooling = new connectionpooling(); myconnectionpooling.Run(); }
public void Run() { try { String connString;
// Specification in the connection string: // Please note: Pooling is implicit, you automatically get it unless you disable it. // Therefore, "true" is the default for the pooling keyword (pooling=true). // Connection Reset: False // Connection Lifetime: 5 // Enlist: true // Min Pool Size: 1 // Max Pool Size: 50 connString = "server=(local)\\NetSDK;Trusted_Connection=yes;database=northwind;" + "connection reset=false;" + "connection lifetime=5;" + "min pool size=1;" + "max pool size=50";
SqlConnection myConnection1 = new SqlConnection(connString); SqlConnection myConnection2 = new SqlConnection(connString); SqlConnection myConnection3 = new SqlConnection(connString);
// Open two connections. Console.WriteLine ("打开两个连接。"); myConnection1.Open(); myConnection2.Open();
// Now there are two connections in the pool that matches the connection string. // Return the both connections to the pool. Console.WriteLine ("将两个连接都返回到池中。"); myConnection1.Close(); myConnection2.Close();
// Get a connection out of the pool. Console.WriteLine ("从池中打开一个连接。"); myConnection1.Open();
// Get a second connection out of the pool. Console.WriteLine ("从池中打开第二个连接。"); myConnection2.Open();
// Open a third connection. Console.WriteLine ("打开第三个连接。"); myConnection3.Open();
// Return the all connections to the pool. Console.WriteLine ("将所有三个连接都返回到池中。"); myConnection1.Close(); myConnection2.Close(); myConnection3.Close(); } catch (Exception e) { // Display the error. Console.WriteLine(e.ToString()); } } }