How to
solve problem “Timeout expired. The timeout period elapsed prior to
obtaining a connection from the pool. This may have occurred because all pooled
connections were in use and max pool size was reached.”
Reason:
Usually do not close its
associated connection when use a DataReader.
Three ways to solve:
1 SqlDataReader sdr =
sCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
2 Explicitly close the connection
sc.Close();
3 Using block
using (SqlConnection sc = new SqlConnection(connString))
{
SqlCommand sCmd = new SqlCommand("SELECT * FROM Shippers", sc);
sc.Open();
Console.WriteLine("Conns opened " + i.ToString());
SqlDataReader sdr =
sCmd.ExecuteReader();
sdr.Close();
}
No comments:
Post a Comment