我一定要弃置SQLiteCommand对象?对象、SQLiteCommand

2023-09-07 02:55:03 作者:半城繁华

我如何对待 SQLiteCommand 的对象, 做我的必须拨打的Dispose()的ExecuteScalar 的ExecuteNonQuery 的ExecuteReader 或不?

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

在 SQLiteCommand 不处理它,而 在 SQLiteTransaction 的例子处置的SQLiteCommand对象。

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.

主要是我很感兴趣,不漏连接或令人不安的SQLite。我知道使用的IDisposable 用法

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

推荐答案

这是最佳实践处置一切器具的IDisposable 只要你用它,因为完成它可能使用的非托管资源。

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

这应该做与使用 - 声明因为它包装使用这个对象的code和,因为其配置也万一出现异常。

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
 
精彩推荐
图片推荐