0
在VS2012我的C#WINFORM構建和我使用SQLSERVER 2012 我使用4個連接字符串,C#應用程序凍結,同時使用多個連接字符串
public SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Database=DB1;Integrated Security=True;User Id=sa;Password=abc");
public SqlConnection conatt = new SqlConnection("Data Source=.\\SQLEXPRESS;Database=DB2;Integrated Security=True;User Id=sa;Password=abc");
public static SqlConnection concard = new SqlConnection("Data Source=.\\SQLEXPRESS;Database=DB3;Integrated Security=True;User Id=sa;Password=abc");
和我開這樣的
關閉連接public void connect()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
if (conatt.State == ConnectionState.Closed)
{
conatt.Open();
}
if (concard.State == ConnectionState.Closed)
{
concard.Open();
}
}
public void disconnect()
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
if (concard.State == ConnectionState.Open)
{
concard.Close();
}
}
,每當我運行,它凍結和一分鐘後,它給出了一個錯誤
ŧ imeout已過期。在從池中獲取連接之前已超時。
所以我用Google搜索並試圖增加池大小,但似乎沒有太大的區別。
我試圖打開並在每次更新密切的聯繫,並插入查詢
例如:
public bool UpdateStatusintblOrder(string orderid, bool cancel)
{
bool isupdated = false;
try
{
SqlCommand cmd = new SqlCommand("Update tblOrder Set [email protected] Where [email protected]", con);
cmd.Parameters.AddWithValue("@cancel", cancel);
cmd.Parameters.AddWithValue("@orderid", orderid);
connect();
if (cmd.ExecuteNonQuery() > 0)
isupdated = true;
}
catch (Exception ex) { MessageBox.Show(ex.Message, "Change Order", MessageBoxButtons.OK, MessageBoxIcon.Error); }
finally { disconnect(); }
return isupdated;
}
但選擇查詢我返回值的DataTable
例如:
public DataTable taxmaster()
{
DataTable dt = new DataTable();
string sqlqry = "Select * From TaxTable ";
try
{
SqlCommand cmd = new SqlCommand(sqlqry, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
return dt;
}
這些所有查詢都在Class文件中。我以各自的形式致電。
所以我做錯了什麼。由於這個問題困擾了我一段時間,我還需要在同一個應用程序中添加一個連接字符串。檢查我的整個代碼沒有任何可能會減緩進程。但是,錯誤表明它與連接有關的錯誤。
所以請指教。
感謝
如果你試圖打開一個連接,那麼它的工作原理和它仍然給你超時錯誤? – dotnetstep 2014-11-08 06:18:42
早些時候有2個連接,它工作得很好。 – mark 2014-11-08 06:22:27
ConnectionPool沒有太大的幫助,因爲它只有在每次都使用相同的連接字符串時才起作用。在這裏你有不同的每個連接字符串,所以它不會幫你。 – dotnetstep 2014-11-08 06:23:53