Using declare to use Connection Connection automatically shut down the recovery, recycling forget to avoid close

Using declare to use Connection Connection automatically shut down the recovery, recycling forget to avoid close


After writing Connection, if Conn after Open Close if not, then it will cause Connection has been attached until GC up to be able to release resources

Such a phenomenon as long as the error handler did not deal with, can easily cause such a result, but this result will not be a burden for Web Server, there is the burden of back-end SQL Server

This creates an interesting phenomenon, when the Web Server and SQL Server a different host, for writing programs on the Web Server is not well controlled, resulting in resources consumption of SQL Server .... but because of the Web Server itself does not have a feeling (loss of a SQL Server), so the Web Server will not start because of insufficient resources to recover resources → GC SQL cause waste of resources and can not be effectively recovered

And to avoid such a situation occurs, .NET Framework 2.0 provides a new way, is to use Using to declare variables, then the time has come to execute End Using the program will automatically do that variable resource recovery

The following look at what is the same way on the wording of the Dim and use Using

Using the Dim statement of way

Dim ConnStr As String = getConnStr()
Dim Conn As New SqlConnection(ConnStr)
Try

Catch ex As Exception
	'错误处理
Finally
	'关闭回收
	Conn.Close()
	Conn.Dispose()
End Try

Use Using statement

Dim ConnStr As String = getConnStr()
Try
	Using Conn As New SqlConnection(ConnStr)

	End Using
Catch ex As Exception
	'错误处理
End Try

Error handling in the program even if there is no deal, do not close the Connection after an error occurred in the implementation of the End Using, they will automatically do the variable resource recycling
can save Conn.Cloase with Conn.Dispose

As for friends asked, Conn.Open if necessary, after which you will use to look at something, if you use Cmmd (Command) of ExecuteReader, ExecuteNonQuerry, then you must perform Conn.Open, if you by Da (SqlDataAdapter) to deal with, it will automatically determine whether Da Conn.Open If not, he would deal with their own.


The following is the signature:

  • Welcome to the site posted the article, but the gist of your posts on the plus - posted, and attach Benpian hyperlinks station name in the article [topcat extreme cohesion between the Shan dance], thanks you for your cooperation.
  • Most small meow meow small article will be familiar language to write VB.NET, C # if you need the Code, perhaps you can try online conversion tool, providing several reference here
    • http://converter.telerik.com/
    • http://www.carlosag.net/tools/codetranslator/
    • http://www.developerfusion.com/tools/convert/vb-to-csharp/

Microsoft MVP
Visual Studio and Development Technologies
(2005~Now)

topcat
Blog:http://www.dotblogs.com.tw/topcat


Original: Large column  using to declare Using Connection Connection to automatically shut down the recovery, recycling forget to avoid close


Guess you like

Origin www.cnblogs.com/petewell/p/11445726.html