使用'使用'处理非托管资源所需的帮助 [英] Help needed on disposing unmanaged resources with 'using'

查看:80
本文介绍了使用'使用'处理非托管资源所需的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我从MSDN网站上获取的这段代码中,我有一个疑问。请帮忙。



In this code which I took from MSDN website, i have a doubt. Please help.

private static void CreateCommand(string queryString,
string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
           connectionString))
    {
        try
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            command.Connection.Open();
            command.ExecuteNonQuery();
        }
        catch (InvalidOperationException)
        {
            //log and/or rethrow or ignore
        }
        catch (SqlException)
        {
            //log and/or rethrow or ignore
        }
        catch (ArgumentException)
        {
            //log and/or rethrow or ignore
        }
    }
}





在这个片段中,是否意味着当我们退出块时,使用块内的所有非托管资源都将被处理掉?



提前感谢。



我尝试过的事情:



做了很多阅读处置的网络e,GC和发布的问题,以澄清我在代码项目中的疑虑。我仍然处于内存管理的几个方面的两难境地。



In this snippet, does it mean that all unmanaged resources inside the using block will get disposed when we exit the block ?

Thanks in advance.

What I have tried:

Did a lot reading on the web on dispose, GC and also posted questions to clarify my doubts here in code project. Im still in dilemma on several aspects of memory management.

推荐答案

没有。只是 SqlConnection连接将被处置。

鉴于 SqlConnection 是正确的,它所占用的任何非托管资源都将可用在结束括号后。但 SqlConnection 类型之外的任何内容都由你来处理。
No. Just the SqlConnection connection will get disposed.
Given the SqlConnection is correct, any unmanaged resources it occupies will be available after the closing bracket. But anything outside the SqlConnection type is up to you to take care of.


这篇关于使用'使用'处理非托管资源所需的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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