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()); } } }