我们在下面的代码中使用 "using statement" 的原因是什么?

The reason we use "using statement" in the following code?

我不能完全理解为什么我们应该在这里使用“using”块

以下代码用于使用 EF

显示数据库中的两个表名称 "genre" 和 "review"

是代码:

C#

protected void Page_Load(object sender, EventArgs e)    
{    
    using (PlanetWroxEntities myEntities = new PlanetWroxEntities())    
    {
        var authorizedReviews = from review in myEntities.Reviews    
                                where review.Authorized == true    
                                orderby review.CreateDateTime descending    
                                select review;

        GridView1.DataSource = authorizedReviews.ToList();

        GridView1.DataBind();    
    }    
}

这里是作者的一些解释:

After the model has been generated, you can execute LINQ queries against it to get data out of the underlying database. To access the data, you need an instance of the DbContext class, which is the base class for the PlanetWroxEntities class. That instance is created inside the Using block in the code. A Using block (using in C#) is used to wrap code that creates a variable that must be disposed of (cleared from memory) as soon as you’re done with it. Because the myEntities variable holds a (scarce) connection to the SQL Server database, it’s a good idea to wrap the code that uses it in a Using block, so the object is destroyed at the end of the block and the connection is released. This myEntities object then exposes your data (such as reviews and genres) that you can use in a query:

我有两个问题:

1- 为什么我们应该去掉变量?是不是因为占用内存space?或者是因为它拥有与 SQL 服务器数据库的连接,所以我们不需要对象而只需要连接?

2- "connection is released" 是什么意思? 如果连接是对象的一部分,为什么它没有被破坏?

PlanetWroxEntities 实施 IDisposable.

IDisposable 由 类 实现,它包含当您使用完某个对象时需要释放的本机资源。

因此,回答 #1:您应该尽快 "get rid" 变量,这样您就不会保留可以释放回系统的本机资源。

using 语句确保在您使用完实体后正确调用 Dispose()。您不仅要释放连接,还要释放任何其他本机资源。

1) 使用using 语句的原因是为了确保myEntities.Dispose() 方法在using 语句中的代码完成后立即被调用。这是必要的,因为 PlanetWroxEntities class' 基础 class 是一个 DbContext,它保持与数据库的连接。

数据库连接是从数据库连接池中获取的宝贵资源,整个应用程序必须共享的可用连接数量有限。如果您不通过 using 语句(或其他方式)在 myEntities 对象上调用 Dispose,那么 myEntities 对象将继续绑定该数据库连接持续一段不确定的时间,直到来自 .net 内存管理器的垃圾收集代码开始回收不再使用的 myEntities 对象所持有的内存。

届时内存管理器会调用dispose方法。一旦调用 Dispose 方法,数据库连接就会从对象中释放并返回到数据库连接池中,当您调用以获取数据库连接时(例如,当您创建PlanetWroxEntities class.)

的其他实例

using 语句的目的是在您使用完对象后立即调用 Dispose 方法,以便在这种情况下立即将数据库连接返回到池中。

2) 这意味着数据库连接被您的应用程序释放并返回到数据库连接池,现在可以被您的应用程序的其他部分获取。这是一个更多地讨论连接池的资源,它将为您提供更多关于幕后发生的事情的背景:https://msdn.microsoft.com/en-us/library/8xx3tyca.aspx

在底层,using 语句是作为 finally 语句实现的。所以这段代码:

using (PlanetWroxEntities myEntities = new PlanetWroxEntities())    
{
    var authorizedReviews = from review in myEntities.Reviews    
                            where review.Authorized == true    
                            orderby review.CreateDateTime descending    
                            select review;

    GridView1.DataSource = authorizedReviews.ToList();

    GridView1.DataBind();    
} 

变成

PlanetWroxEntities myEntities; 
try{
    myEntities = new PlanetWroxEntities();
    var authorizedReviews = from review in myEntities.Reviews    
                            where review.Authorized == true    
                            orderby review.CreateDateTime descending    
                            select review;

    GridView1.DataSource = authorizedReviews.ToList();

    GridView1.DataBind();   

} finally{
    myEntities.Dispose();
}

在过去的 C# 时代,我们总是不得不自己编写 finally 语句来处理一次性对象,以便释放它们的非托管资源。但是后来在C#中引入了using语句来更容易的实现这一点。即使在今天,如果愿意,您也可以自己编写 finally 语句,但大多数人都喜欢 using 语句。

有关详细信息,请参阅 https://msdn.microsoft.com/en-us/library/yh598w02.aspx

为什么要去掉变量?

is it because it takes memory space?

or is it because it holds a connection to the SQL Server database so we don't need object but just the connection?

作业(填充网格)在 GridView1.DataBind();

内完成

之后您不需要对象或连接。

What does it means "connection is released"? if the connection is part of object why it didn't destroy too?

对象 myEntities 及其与数据库的连接被销毁。

通常情况下,EF 查询返回的实体可以生成进一步的数据库查询。这称为 延迟加载

在 using 语句中包装 EF 查询可防止发生延迟加载 - 让您了解数据库查询仅在存储库中发生。