c# - 配置连接、命令、适配器

标签 c# .net using oledbconnection disposing

当我从我的 C# 代码查询数据库时,我通常使用类似这样的结构:

using (var connection = new OleDbConnection(connStr))
using (var command = new OleDbCommand(query, connection))
using (var adapter = new OleDbDataAdapter(command))
{///}

我应该实际使用所有这些“使用”,还是仅处理连接就足够了,所有相关对象也将被处理?

最佳答案

it will be enough to dispose only connection and all related objects will be disposed too?

没有。处置连接只会处置连接对象。

通常,释放每个实现了 IDisposable 的对象是一种安全的做法。 (还有一点,处理 Command 对象不会处理相关的连接对象)

关于c# - 配置连接、命令、适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28857813/

相关文章:

c# - 如何在 visual studio 中使用 C# android 创建 android 小部件

c# - 如何通过 OpenXML 在当前事件的 Word 文档中设置自定义属性

.net - 如何使用 nHibernate 映射 n 列主键

templates - C++ 模板别名的等价性

c# - JsonServiceClient的OnUploadProgress可以和.WithCache()结合使用吗?

c# - 未找到以下命令 netsh

c# - String.IndexOf 的 IList<T> 版本(找到一个子 -'string' ,而不仅仅是单个对象)

.net - 在 PerfMon 实例中选择正确的 AppPool

c# - 多次处理对象

templates - C++11 中迭代器的类型别名/using 声明