关于SqlConnection.Close()和Dispose()方法该何时调用
这是一个老生常谈的问题了,但是看到这里经常会有各种各样的回答。有的说用完之后两个方法最好都调用一下,有的说不调用的话就会导致连接池满,……
总之,似乎并没有统一的标准答案。让我们先看看MSDN吧:http://msdn.microsoft.com/en-us/library/ff647768.aspx
这上面提到的关闭数据库连接的两种标准做法:
显而易见,我们并不需要同时调用两个方法,只要调用Close()或者Dispose()中的任意一个就可以了,理由很简单,看看Dispose()的实现就知道了。
也就是说,调了Dispose()方法之后,它会自动调用Close()方法的。其实,对于Close()来说,并不是真正地把数据库连结给关闭了,只是放回到的连接池而已;这一点,很多人会误以为调用了Close()就是把连接关闭了。
那么,如果不调用Dispose()或者Close()又会怎样呢?
当你的程序并不需要频繁地操作数据库的时候,就算忘记调用Dispose或者Close也无伤大雅,因为SqlConnection类实现了Finalize方法,而Finalize方法里面又调用了Dispose。所以,最终Finalizer Queue在回收资源的时候也会调用到SqlConnection的Close()方法的。
其实,关于这一切MSDN上面都说得很清楚:
•Using either the Close method or the Dispose method is sufficient. You do not have to call one method after the other. There is no benefit to calling one method after the other.
•Dispose internally calls Close. In addition, Dispose clears the connection string.
•If you do not call Dispose or Close, and if you do not use the using statement, you are reliant upon the finalization of the inner object to free the physical connection.
•Use the using statement, instead of Dispose or Close, when you are working with a single type, and you are coding in Visual C#®. Dispose is automatically called for you when you use the using statement, even when an exception occurs.
•If you do not use the using statement, close connections inside a finally block. Code in the finally block always runs, regardless of whether an exception occurs.
•You do not have to set the SqlConnection reference to null or Nothing because there is no complex object graph. Setting object references to null or to Nothing is usually done to make a graph of objects unreachable.
总之,似乎并没有统一的标准答案。让我们先看看MSDN吧:http://msdn.microsoft.com/en-us/library/ff647768.aspx
这上面提到的关闭数据库连接的两种标准做法:
public void DoSomeWork()
{
SqlConnection conn = new SqlConnection(connectionString);
try
{
conn.Open();
// Do Work
}
catch (Exception e)
{
// Handle and log error
}
finally
{
if(null!=conn)
conn.Close();
}
}
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
. . .
} // Dispose is automatically called on the conn variable here
显而易见,我们并不需要同时调用两个方法,只要调用Close()或者Dispose()中的任意一个就可以了,理由很简单,看看Dispose()的实现就知道了。
/// <summary>
/// Releases all resources used by the <see cref="T:System.ComponentModel.Component" />.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
this._userConnectionOptions = null;
this._poolGroup = null;
this.Close();
}
this.DisposeMe(disposing);
base.Dispose(disposing);
}
也就是说,调了Dispose()方法之后,它会自动调用Close()方法的。其实,对于Close()来说,并不是真正地把数据库连结给关闭了,只是放回到的连接池而已;这一点,很多人会误以为调用了Close()就是把连接关闭了。
那么,如果不调用Dispose()或者Close()又会怎样呢?
当你的程序并不需要频繁地操作数据库的时候,就算忘记调用Dispose或者Close也无伤大雅,因为SqlConnection类实现了Finalize方法,而Finalize方法里面又调用了Dispose。所以,最终Finalizer Queue在回收资源的时候也会调用到SqlConnection的Close()方法的。
其实,关于这一切MSDN上面都说得很清楚:
•Using either the Close method or the Dispose method is sufficient. You do not have to call one method after the other. There is no benefit to calling one method after the other.
•Dispose internally calls Close. In addition, Dispose clears the connection string.
•If you do not call Dispose or Close, and if you do not use the using statement, you are reliant upon the finalization of the inner object to free the physical connection.
•Use the using statement, instead of Dispose or Close, when you are working with a single type, and you are coding in Visual C#®. Dispose is automatically called for you when you use the using statement, even when an exception occurs.
•If you do not use the using statement, close connections inside a finally block. Code in the finally block always runs, regardless of whether an exception occurs.
•You do not have to set the SqlConnection reference to null or Nothing because there is no complex object graph. Setting object references to null or to Nothing is usually done to make a graph of objects unreachable.
...全文
相关推荐
asp.net sqlconnection con.close和con.dispose区别
con.close是用来关闭和数据库的连接,相对于open
C#中析构函数、Dispose、Close方法的区别
本文详细对比了C#中析构函数、Dispose和Close方法的区别,三者都是释放资源,本文介绍了他们各自的使用方法和使用场景,希望对大家有所帮助。
C#使用SqlConnection连接到SQL Server的代码示例
主要介绍了C#使用SqlConnection连接到SQL Server的代码示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
C#连接SQL Server数据库的实例讲解
在本篇文章里小编给大家整理了关于C#连接SQL Server数据库的实例内容,有需要的朋友们参考学习下。
ADO.NET连接池示例
ADO.NET链接对象(SqlConnection或者OracleConnection)默认情况都开启连接池(平时编程时可能会忽略)。当我们调用Close或者Dispose方法时,实际并不断开连接,而是把连接放回连接池,再次使用时候重连接池中取得空闲资源。因为打开和关闭数据库连接开销比较大,所以连接池对于与数据库链接资源的控制上,加快客户端程序的效率都有很大地帮助。 示例中会看到如何控制连接池个数,链接对象关闭连接放回链接池中,有其他连接对象打开连接时重连接池中取得空闲的对应连接。


