我創建了一個查詢db2數據庫並返回信息的小應用程序。我創建了一個接受輸入並從查詢中返回信息的窗體。我的總結陳詞是:關閉窗體關閉SQL連接(C#)
finally { conn.close(); }
我很好奇 - 不連接(康涅狄格州)實際上接近時,我打窗體上的小紅盒子?我在這裏和網上搜索了其他問題,但無法找到明確的答案。
下面是完整的try-catch-finally塊(有一些信息模糊 - > *****):
`嘗試 {
conn.Open();
string queryString = String.Format("SELECT * " +
"FROM ***** " +
"WHERE USERPRF LIKE '%{0}%' " +
"ORDER BY TIMESTMP DESC " +
"FETCH FIRST 1 ROWS ONLY", userNameInput);
using (OdbcCommand com = new OdbcCommand(queryString, conn))
{
using (OdbcDataReader reader = com.ExecuteReader())
{
if (reader.Read())
{
string ***** = reader["*****"].ToString();
string ***** = reader["*****"].ToString();
string user = reader["USERPRF"].ToString();
string timeStamp = reader["TIMESTMP"].ToString();
listBox1.Items.Clear();
listBox1.Items.Add("Username: " + user);
listBox1.Items.Add("*****" + *****);
listBox1.Items.Add("*****: " + *****);
listBox1.Items.Add("Last Changed: " + timeStamp);
}
else
{
listBox1.Items.Clear();
listBox1.Items.Add("There was no data to return! Try again.");
}
}
}
}
catch (Exception ex)
{
string errorMessage = ex.Message;
}
finally
{
conn.Close();
}`
如果你的應用程序是一個單一的窗體應用程序,並關閉你的應用程序,conn將明確不會保持打開狀態。但是您將其作爲try catch塊的一部分展示,因此我們需要查看完整的try catch來告訴您發生了什麼。 – nabuchodonossor