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 ?
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 ?
Dispose
also closes the connection if it hasn't been closed, but when callingClose
, 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 ausing
block:Connection.Close() will simply close the connection to the server as defined in the connection string. The Connection can be used/re-opened after this point.
Connection.Dispose()
will clean up completely, removing all unmanaged resources preventing that Connection from being used again. Once disposed is called you shouldn't try to use the object any more. WithinDispose(),
Close()` will all most certainly be called too.I would recommend
using
the using syntax like so if possible, to ensure things are cleaned up correctly:This will automatically dispose of the connection for you, regardless of an exception being thrown.