首页蓝耳朵|小小蓝耳朵广州图书馆外文室英语儿童读物资源介绍网佛教青年之友旧版收集永硕E盘Phonics Short Vowels Game 
Close、Dispose、using
所属栏目:ASP.NET(webform)  时间:2010-07-29 11:57  作者:狼子

对于IDisposable对象,使用using可以自动调用Dispose(),在using语句块里,发生了异常,也会自动调用Dispose(),因为return语句使程序离开了using语句块,所以也会调用Dispose()

对于数据库连接,Dispose()会自动调用Close(),还会把所有因为关联的变量都清空,包括ConnectionString,Close()对未提交的事务做回滚处理,把连接归还给连接池

所以,直接使用using是最好的,用using,就不需要用Dispose(),也不需要Close()了,就是,对非IDisposable对象,不可以调用using语句,如果要手动释放资源,要自己调用Dispose()

记录下面的连接:

浅析SqlConnection的dispose和close方法差异
http://developer.51cto.com/art/200906/129419.htm

引用微软ADO.Team的经理的话说,sqlconnection的close和dispose实际是做的同一件事,唯一的区别是Dispose方法清空了connectionString,即设置为了null.

不管是dispose还是close都不会销毁对象,即不会释放内存,它们会把sqlconnection对象丢到连接池中

所以在try..catch和using的选择上大胆的使用using吧

using不会捕捉其代码快中的异常,只会最后执行dispose方法

下面是我按文章中的测试方法测试的,和文章中讲的一样子。。。Dispose释放资源,原来释放的是占用的资源,定义的所有变量还是存在的

SqlConnection dbConnect = new SqlConnection(NnllData.dbConnectionString);
        dbConnect.Open();
        Response.Write("<p>Open:" + dbConnect.ConnectionString + ",State:" + dbConnect.State.ToString() + "</p>");
       
        dbConnect.Close();
        Response.Write("<p>Close:" + dbConnect.ConnectionString + ",State:" + dbConnect.State.ToString() + "</p>");
       
        dbConnect.Open();
        Response.Write("<p>Open:" + dbConnect.ConnectionString + ",State:" + dbConnect.State.ToString() + "</p>");

        dbConnect.Dispose();
        Response.Write("<p>Dispose:" + dbConnect.ConnectionString + ",State:" + dbConnect.State.ToString() + "</p>");

        dbConnect.ConnectionString = NnllData.dbConnectionString;
        dbConnect.Open();
        Response.Write("<p>Open:" + dbConnect.ConnectionString + ",State:" + dbConnect.State.ToString() + "</p>");

        dbConnect.Dispose();


SqlConnection.Close Method
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close(v=VS.71).aspx

Closes the connection to the database. This is the preferred method of closing any open connection.

The Close method rolls back any pending transactions. It then releases the connection to the connection pool, or closes the connection if connection pooling is disabled.

It is obvious we had to close the connection everytime we finish our job. The connection of database is limited, we can't just turn them on and never shut them off. But I didn't know the Close method releases the connection to the connection pool, it is so cool. No wonder the first time we connect the database always last longer.


SqlConnection.Dispose Method (Boolean)
http://msdn.microsoft.com/en-us/library/aa326261(VS.71).aspx

This method is called by the public Dispose() method and the Finalize method.

The Dispose method calls Close, and returns the SqlConnection to the connection pool.

Now if call Dispose, it calls Close...


IDisposable.Dispose Method
http://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx

Use this method to close or release unmanaged resources such as files, streams, and handles held by an instance of the class that implements this interface. By convention, this method is used for all tasks associated with freeing resources held by an object, or preparing an object for reuse.

When implementing this method, ensure that all held resources are freed by propagating the call through the containment hierarchy. For example, if an object A allocates an object B, and object B allocates an object C, then A's Dispose implementation must call Dispose on B, which must in turn call Dispose on C. An object must also call the Dispose method of its base class if the base class implements IDisposable.

To SqlConnection.Dispose Method, it means whatever object assigned with SqlConnection would be free together. So is ConnectionString.


using Statement (C# Reference)
http://msdn.microsoft.com/en-us/library/yh598w02.aspx

Provides a convenient syntax that ensures the correct use of IDisposable objects.

As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler.

So we can put using statement on an IDisposable object, then the object would dispose itself when leaving its using block, even when quiting by Exception.

小楼宝宝的涂鸦花花(Imitater)的博客起名称骨测字皖ICP备06000023号-17