日期:2014-05-19  浏览次数:20343 次

新手求救using的简单问题
请问下面一段代码using的作用

using   (SqlConnection   connection   =   new   SqlConnection(connectionString))

------解决方案--------------------
在{}结束后自动释放资源
------解决方案--------------------
是用于自动释放资源

------解决方案--------------------
using (SqlConnection connection = new SqlConnection(connectionString))
{

}
//对象释放
------解决方案--------------------
在离开语句块时自动调用Dispose。
------解决方案--------------------
using (SqlConnection connection = new SqlConnection(connectionString))
{

}

这应该等于
try
{
SqlConnection connection = new SqlConnection(connectionString);
}
catch
{

}
finally
{
connection.dispose();
}

------解决方案--------------------
SqlConnection在dispose的时候,会检查有没有DataReader对象还在连接,是否已经Close,并且会依次对DataReader执行Close,然后对自己执行Close将连接放回缓冲池。