【发布时间】:2015-11-13 16:25:53
【问题描述】:
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;
}
}
我的问题是,如果我删除 finally 代码块,垃圾收集器会在处理 SQlConnection 对象时关闭连接吗?
我知道明确表达是一种更好的做法,但我的同事不同意。
【问题讨论】:
-
你的同事错了
-
为什么不包装
Sql Objects around a using(){}以利用自动处理的优势.. GC 也不总是即时的.. 我也同意@Steve 也许你不需要依赖你的奶牛工人给你有缺陷的建议/信息 -
为什么不让它保持原样,代码看起来非常好......
标签: c# dispose sqlconnection