What is the difference between connection.Close() and connection.Dispose()?

Epoc picture Epoc · Jun 18, 2013 · Viewed 30.4k times · Source

I noticed that the SQLiteConnection object in System.Data.SQLite owns two similar methods :

  • Close()
  • Dispose()

Same for the SQLiteDataReader object.

What is the difference ?

Answer

Steven picture Steven · Jun 18, 2013

Dispose also closes the connection if it hasn't been closed, but when calling Close, you can reopen the connection again. This is not possible when the connection is disposed.

In general, don't call Close, but simply call dispose implicitly by wrapping the creation of a connection in a using block:

using (var connection = new SqlConnection(...))
{
    // use connection here.
} // connection gets closed and disposed here.