Do I have to Dispose the SQLiteCommand objects?

后端 未结 4 1912
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-18 08:33

How do I treat the SQLiteCommand object, do I have to call Dispose() after ExecuteScalar, ExecuteNonQuery

4条回答
  •  天涯浪人
    2021-01-18 08:55

    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
    

提交回复
热议问题