[問題] using SqlConnection 問題

看板C_Sharp (C#)作者 (winsummer)時間12年前 (2013/01/15 12:24), 編輯推噓1(103)
留言4則, 3人參與, 最新討論串1/1
各位好 想請教一個問題 public sealed class ConnectionManager { public static SqlConnection GetWebConnection() { string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); return connection; } } =============================================== 請問如果我這樣寫 using (SqlCommand command = new SqlCommand(sql, ConnectionManager.GetWebConnection())) { //my statment } 是不是不用再close connection? 感謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 202.175.124.66

01/15 12:58, , 1F
對,但不是個好習慣
01/15 12:58, 1F

01/16 14:20, , 2F
請問不是好習慣的原因是?
01/16 14:20, 2F

01/16 14:20, , 3F
using不就是為了方便這樣使用而產生的嗎?
01/16 14:20, 3F

01/16 18:34, , 4F
我覺得只要new IDispose的元件就用using才是好習慣
01/16 18:34, 4F
文章代碼(AID): #1GzDeGhQ (C_Sharp)
文章代碼(AID): #1GzDeGhQ (C_Sharp)