`
shirlly
  • 浏览: 1625014 次
  • 性别: Icon_minigender_2
  • 来自: 福州
社区版块
存档分类
最新评论

SqlConnection - Close() or Dispose()

    博客分类:
  • .NET
阅读更多
There seems to be a lot of confusion about how to clean up after using a SqlConnection object. Should you call Close(), Dispose(), Close() then Dispose(), or neither?

Here are some relevant facts we need to consider:


When an open SqlConnection goes out of scope, its underlying physical database connection will not be closed or returned to the connection pool by the garbage collector;
Dispose() always calls Close() implicitly;
Dispose() clears the ConnectionString, Close() does not;
In future versions of ADO.NET, the SqlConnection.Dispose method might free other unmanaged resources, in addition to the database connection.
What conclusions can we draw?


We must at least call Close() or Dispose(), otherwise the database connection won't be released;
There's no need to call both Close() and Dispose();
If we're going to open the connection again, we should Close() not Dispose();
When we're completely finished with the SqlConnection, we should call Dispose() to make sure that all unmanaged resources are released, both now and in the future.

The tempation of symmetry after calling Open() is to always call Close(), as was the case in classic ADO, but I've shown that in the case of SqlConnection we only need to call Dispose(). Better still, make sure Dispose() is always called implicitly by enclosing your SqlConnection objects in a using statement.

原文连接:http://chrisfulstow.blogspot.com/2006/11/sqlconnection-close-or-dispose.html


con.close() 是关闭连接,实际上是把连接放回ado.net的连接池,并没有真正关闭,所以再次连接时只是把连接从池中拿出来用,速度很快。
con.dispose是用来释放对象的所在内存,相对于new sqlconnection();
只用dispose是不能关闭connection的,两者不是一回事,只用close也不能释放它所占的内存。
conn.dispose() 是销毁连接,彻底关闭。
本文来自: 脚本之家(www.jb51.net) 详细出处参考:http://www.jb51.net/article/16923.htm
分享到:
评论

相关推荐

    asp.net sqlconnection con.close和con.dispose区别

    con.dispose是用来释放对象的所在内存,相对于new sqlconnection(); 只用dispose是不能关闭connection的,两者不是一回事,只用close也不能释放它所占的内存。 conn.dispose() 是销毁连接,彻底关闭。 您可能感兴趣...

    【第一讲连接数据库】-01B-SqlConnection对象使用.rar )

    【第一讲连接数据库】-01B-SqlConnection对象使用.rar )

    delphi 连接 mysql sqlconnection

    delphi 使用sqlconnection 连接 mysql 类

    数据库连接助手 SqlConnection

    SqlServer 数据库 连接助手 [全局]--打开数据库,传递参数无;返回参数:SqlConnection MyData_Mydb

    C# SqlConnection

    自己常用的C# Database 类。包括 SqlConnection,SqlCommand,SqlDataReader 的使用

    sqlConnection对象

    sqlConnection对象,教你如何学会sqlConnection的操作。。仅仅收1分,回复即可取回。。全部是自己手写的

    c#数据库操作的3种典型用法

    sqlConnection.Close(); } public void UseSqlStoredProcedure() { SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand); SqlCommand sqlCommand = new SqlCommand();...

    sqlconnection

    通用的数据库(sqlite/mysql/postgresql)链接库,使用c++14封装数据库的底层接口,提供统一的c++调用方式,更为少量的代码、方便、灵活并且跨平台。

    ADO.NET连接池示例

    当我们调用Close或者Dispose方法时,实际并不断开连接,而是把连接放回连接池,再次使用时候重连接池中取得空闲资源。因为打开和关闭数据库连接开销比较大,所以连接池对于与数据库链接资源的控制上,加快客户端程序...

    .net 中的SqlConnection连接池机制详解

    很多人认为 SqlConnection 的连接是不耗时的,理由是循环执行 SqlConnection.Open 得到的平均时间几乎为0,但每次首次open 时,耗时又往往达到几个毫秒到几秒不等,这又是为什么呢? 首先我们看一下 MSDN 上的权威...

    C#源代码 学生成绩管理系统

    SqlConnection con=new SqlConnection(DataAccess.ConnectionString); SqlCommand cmd=new SqlCommand(sql,con); SqlDataReader dr=null; try { con.Open(); dr=cmd.ExecuteReader...

    SqlConnection_Class.zip_SqlConnection A_连接数据库

    有关于SQL连接数据库的类,提供了简单的接口,很适合二次开发

    c++ 学生管理系统

    sc = gcnew SqlConnection(); sc->ConnectionString=L"Data Source=OCYOFXAHSZFQJMN;Integrated Security=true;Initial Catalog=STUDENT "; sc->Open(); cmd = gcnew SqlCommand(); cmd->...

    DBHelper方便连接数据库 DBHelper

    private static SqlConnection connection; public static SqlConnection Connection { get { string connectionString = ConfigurationManager.ConnectionStrings["MyOfficeConnectionString"]....

    解析如何正确使用SqlConnection的实现方法

    本篇文章对如何正确使用SqlConnection的实现方法进行了详细的分析介绍,需要的朋友参考下

    C#使用SqlConnection连接到SQL Server的代码示例

    主要介绍了C#使用SqlConnection连接到SQL Server的代码示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    CMS.DBUtility.dll

    using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { connection.Open(); cmd.CommandTimeout = Times; ...

    XMLHTTPRequest、IDHTTP、SQLConnection访问DataSnap/Restful接口服务演示程序

    XMLHTTPRequest、IDHTTP、SQLConnection访问DataSnap/Restful接口服务演示程序,包含网站安全认证功能

Global site tag (gtag.js) - Google Analytics