处理数据连接是.NET编程的基本之基本,办法很多,有在open前判断是否关闭的,有用reader时加commandbehavior的,有在finally中经判断来释放资源保证关闭连接的,以我的经验而言下面的最为省资源:
using (SqlConnection conn = new SqlConnection(connectingString))<?XML:NAMESPACE PREFIX = O />
{
conn.Open();
try
{
/* do sth.*/
}
catch (***Exception ex)
{
/* handle *** exception */
// Although you really should try limit the catch
// statement to specify ONLY the type of exception
// you are prepared to handle, and NOT catch ALL
// exceptions.
}
}
转载于:https://blog.51cto.com/zhangdalei/54873