string connString; connString = "server=(local);Integrated Security=SSPI;database=northwind;" + "pooling=true;"; SqlConnection myConnection = new SqlConnection(connString); myConnection.Open(); myConnection.Close();
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)\\SQLExpress;Integrated Security=SSPI;database=northwind;" + "connection reset=false;" + "min pool size=1;" + "max pool size=50"; SqlConnection myConnection1 = new SqlConnection(connString); SqlConnection myConnection2 = new SqlConnection(connString); SqlConnection myConnection3 = new SqlConnection(connString);
public class ConnectionPoolingExample { public void Run() { string connString; connString = "server=(local);Integrated Security=SSPI;database=northwind;" + "connection reset=false;" + "min pool size=1;" + "max pool size=50"; SqlConnection myConnection1 = new SqlConnection(connString); SqlConnection myConnection2 = new SqlConnection(connString); SqlConnection myConnection3 = new SqlConnection(connString); // 打開2個鏈接。一個是從鏈接池打開(參考 min pool size),另外一個從數據源建立。 Console.WriteLine("打開2個鏈接。"); myConnection1.Open(); myConnection2.Open(); // 目前,鏈接池裏有2個和鏈接串匹配的鏈接 Console.WriteLine("返回2個鏈接到鏈接池。"); myConnection1.Close(); myConnection2.Close(); // 從鏈接池取出1個鏈接 Console.WriteLine("從鏈接池打開1個鏈接。"); myConnection1.Open(); Console.WriteLine("從鏈接池取出第2個鏈接。"); myConnection2.Open(); Console.WriteLine("第3個鏈接從數據源建立。"); myConnection3.Open(); // 回收3個鏈接到鏈接池 Console.WriteLine("回收3個鏈接到鏈接池。"); myConnection1.Close(); myConnection2.Close(); myConnection3.Close(); } }