SqlConnection - Close() or Dispose()

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SqlConnection是一个在C#中用于与数据库建立连接的类。它可以通过提供连接字符串来创建一个与数据库的连接。例如,使用以下代码创建一个SqlConnection对象: SqlConnection sqlconnection=new SqlConnection(connString); [1] 在创建SqlConnection对象之前,需要先引入相应的命名空间。对于访问MySQL数据库,需要引入MySql.Data.MySqlClient命名空间。例如,在C#代码中,可以使用以下代码创建SqlConnection对象并连接到MySQL数据库: using MySql.Data.MySqlClient; // ... MySqlConnection con = new MySqlConnection(sqlstr); [2] 在连接数据库时,可能会出现异常。为了处理异常,可以使用try-catch语句来捕获并处理异常。以下是一个示例代码,展示了如何在连接数据库时使用try-catch语句: try { con.Open(); // 打开数据库成功的处理逻辑 } catch (Exception ex) { // 打开数据库失败的处理逻辑 } finally { con.Close(); // 关闭数据库连接的处理逻辑 } [2] 综上所述,SqlConnection是在C#中用于与数据库建立连接的类。使用它可以创建一个与数据库的连接,并通过try-catch语句来处理连接过程中可能出现的异常。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [第二周学习笔记(ADO中SqlConnection和SqlCommand的应用)](https://blog.csdn.net/Dennis_t/article/details/88384876)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C#编程基础SqlConnection类的作用以及常用方法](https://blog.csdn.net/ba_wang_mao/article/details/106751820)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值