public CategorieEquipement Select(int NoType)
{
SqlConnection cx = new SqlConnection(WebConfigurationManager.ConnectionStrings["SQLConnect"].Connection String);
SqlDataReader reader;
CategorieEquipement lstCategorie = new CategorieEquipement();
try
{
cx.Open();
SqlCommand com = new SqlCommand("SELECT_CategorieEquip", cx);
com.CommandType = System.Data.CommandType.StoredProcedure;
com.Parameters.AddWithValue("@where",NoType);
reader = com.ExecuteReader();
while (reader.Read())
{
lstCategorie.CodeRef = reader["CodeRef"].ToString();
}
}
catch (Exception ex)
{
Debug.WriteLine("SELECT ERROR : " + ex.ToString());
return null;
}
finally
{
if (cx != null)
{
cx.Close();
}
}
return lstCategorie;
}
}
我的問題是,如果我刪除了代碼塊,垃圾回收器會在處理SQlConnection對象時關閉連接嗎?SqlConnection是否使用此功能處理
我知道這是一個更好的做法是明確的,但我的同事不同意。
你的同事是錯的 – Steve
爲什麼不圍繞一個using(){}'包裝'Sql對象來利用自動配置.. GC並不總是立即要麼..我同意@Steve也許你需要不要依靠你的牛工給你提供有缺陷的建議/信息 – MethodMan
爲什麼不把它保留下來,代碼似乎完全沒問題...... –