简体   繁体   English

DBCommand使用块

[英]DBCommand using block

I have the following code: 我有以下代码:

using (MySqlConnection conn = new MySqlConnection(connStr))
{
    conn.Open();

    cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT * FROM Events";
    cmd.CommandType = CommandType.Text;
    cmd.ExecuteNonQuery();
}

From reading a few articles on this site, it is suggested that the DbCommand should be in a using block, however, I can't see why this is needed. 通过阅读该站点上的几篇文章,建议DbCommand应该在using块中,但是,我看不到为什么需要这样做。 The Connection is closed, so what is DbCommand holding on to that requires a using block? 连接已关闭,那么DbCommand坚持使用的是什么需要using块? Is it really the case if a class inherits from IDisposable that you must use a using block or manually called Dispose? 如果从IDisposable继承的类必须使用using块或手动调用Dispose确实是这种情况?

I ran a simulator with 100 threads on the code above, and also with code with a using block on the DbCommand and I could see no real differences in memory usage. 我在上面的代码上运行了一个具有100个线程的模拟器,并且在DbCommand上使用了带有using块的代码,但我发现内存使用没有真正的区别。

DbCommand is abstract, and does not presuppose what native resources the vendor-specific subclass will hold, and in what order they should be released. DbCommand是抽象的,并且不预设特定于供应商的子类将拥有哪些本机资源,以及应该以什么顺序释放它们。 Proper nesting of using blocks seems a reasonable implicit coding convention. using块的正确嵌套似乎是合理的隐式编码约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM