这是一个老生常谈的问题了,但是看到这里经常会有各种各样的回答。有的说用完之后两个方法最好都调用一下,有的说不调用的话就会导致连接池满,……总之,似乎并没有统一的标准答案。让我们先看看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.

解决方案 »

  1.   

    按照最小设计,Close方法根本就不应该出现,明显两个方法做了同样的事,还有一点,using()只会自动调用Dispose,这也是Close应该去除的理由。
    也许微软程序员考虑到File类若将关闭文件操作称谓Dispose,很怪异,所以还是加上了这个冗余吧,毕竟.NET平台还要支持很多其他语言。
      

  2.   

    也就是说,调了Dispose()方法之后,它会自动调用Close()方法的。其实,对于Close()来说,并不是真正地把数据库连结给关闭了,只是放回到的连接池而已;这一点,很多人会误以为调用了Close()就是把连接关闭了。
      

  3.   

    我也一般用using
    或者 conn.Close();微软自己的代码就这样写了,大家还怕什么.
      

  4.   

    <script>alert('123');</script>我要看看这句话会被弹窗不
      

  5.   

    养成好习惯,使用完后CLOSE掉啊!
      

  6.   

    現在要麽就用Using
    要麽就close();dispose();
      

  7.   

    推荐使用try、 catch conn.Close()
    这样可以扑捉异常,
    用using没有办法扑捉异常,而且用using 必须的实现IDisposible接口 运行完成后他会把连接释放,下次需要重新连接
      

  8.   


    这位同学,你千万不要误导人家啊。你的前半部分关于“不能捕捉异常”是正确的,至于后面的“把连接释放,下次需要重新连接”就是完全错误的了。用了using之后,相当于以下的代码SqlConnection conn = new SqlConnection(connString);
    try
    {
      conn.Open();
    }
    finally
    {
      conn.Dispose();
    }
    最终会调用到Dispose()方法。对于SqlConnection来讲,调用Dispose或者Close的本质是差不多的,最终都是把连接返回到连接池里面。请你仔细阅读并理解内部原理之后再发表高见,呵呵。
      

  9.   

    除了DataReader需要关闭 其它都不用考虑
      

  10.   

    close和dispose可以同时使用吗,会有问题吗