我一定要弃置SQLiteCommand对象? [英] Do I have to Dispose the SQLiteCommand objects?

查看:183
本文介绍了我一定要弃置SQLiteCommand对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何对待 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?

在<一个文档的例子href="http://www.devart.com/dotconnect/sqlite/docs/Devart.Data.SQLite~Devart.Data.SQLite.SQLiteCommand.html"相对=nofollow> SQLiteCommand 不处理它,而 在<一个href="http://www.devart.com/dotconnect/sqlite/docs/Devart.Data.SQLite~Devart.Data.SQLite.SQLiteTransaction.html"相对=nofollow> 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

这篇关于我一定要弃置SQLiteCommand对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆