Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to Dispose the SQLiteCommand objects?

How do I treat the SQLiteCommand object, do I have to call Dispose() after ExecuteScalar, ExecuteNonQuery and ExecuteReader or not?

The documentation example on SQLiteCommand doesn't dispose it whilst in the SQLiteTransaction the example disposes the SQLiteCommand object.

I always close the data reader object though. My application accesses the db from many threads.

Mostly I am interested in not leaking connections or disturbing SQLite. I am aware of using and IDisposable usage

like image 551
Odys Avatar asked Mar 14 '13 11:03

Odys


2 Answers

If it is disposable, dispose it if you will not use it again. The best would be, to use using

using(SQLiteCommand cmd as new SQLiteCoammand())
{
   ...
}

So it will be disposed automatically when leaving the using scope.

like image 29
Chris Avatar answered Sep 20 '22 23:09

Chris


It's best-practise to dispose everything that implements IDisposable as soon as you're finished with it because it might use unmanaged resources.

This should be done with the using-statement since it wraps the code that uses this object and because it disposes it also in case of an exception.

using(var con = new SQLiteConnection(conString))
using(var cmd = new SQLiteCommand(con))
{
    con.Open();
    // ...
} // also closes the connection
like image 159
Tim Schmelter Avatar answered Sep 21 '22 23:09

Tim Schmelter