![](/img/trans.png)
[英]Getting Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. .exception
[英]Getting Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool
我更經常遇到這個超時問題。 我一直在使用MYSQL數據庫並在Windows應用程序上工作。 我什至嘗試過使用Have子句,但面臨相同的情況
public bool VerifyStock(string serialnumber)
{
con = new MySqlConnection(connstring);
string readData = "select * from Fn_Inventory where ModelNumber = '" + serialnumber + "'";
cmd = new MySqlCommand(readData, con);
cmd.Parameters.AddWithValue("@ModelNumber", serialnumber);
con.Open();
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
//while (dr.Read())
if (dr.Read())
{
decimal invquntity = Convert.ToDecimal(dr["AvailableQuantity"].ToString());
decimal quantity = Convert.ToDecimal(txtQuantity.Text);
decimal sinvquntity = invquntity - quantity;
if (sinvquntity >= 0)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
else
{
return false;
}
con.Close();
}
快速增加連接池的大小和超時。 您可以在這里參考以獲取更多信息
並正確關閉打開的連接和末端外部條件。 使用try catch finally
塊並將此代碼添加到finally
塊中,以便始終執行該代碼。
if (con.State == ConnectionState.Open)
{
con.Close();
}
您正在打開連接但不根據條件關閉連接,而是使用return語句從代碼返回,或者在您使用return語句的地方關閉連接,或者使用using()子句。當連接保持打開狀態時,將沒有可用的連接,因此池會滿的。 從數據讀取器讀取值后,請始終關閉連接。
using (SqlConnection con = new SqlConnection()) { //your code here}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.