Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - Re: How to close connection?

 
Thread Tools Search this Thread
Old 11-02-2009, 08:18 PM   #1
Default Re: How to close connection?



"aspfun via DotNetMonster.com" <u53138@uwe> wrote in message
news:9e871b9bd80e9@uwe...
>I used code below to close connection, is it right?
>
> Try
> myConn.Open()
> mySQLCommand.ExecuteNonQuery()
> Catch ex As Exception
> 'code to display error message
> Finally
> myConn.Close()
> mySQLCommand.Dispose()
> myConn.Dispose()
> End Try
>
> Does it need " mySQLCommand.Dispose()"?
>
> --
> Message posted via http://www.dotnetmonster.com


If you are going to dispose the connection, then that will cause it to be
closed if needed, so closing and disposing the connection, while not
incorrect, is overkill.

Also, since your command is tied to an explicit connection, there isn't a
need to dispose of the command. Again, it's not wrong, just overkill in
this case.

You could avoid the dispose calls completely, by declaring your connection
in a Using block as in:

Using myConn As New SqlConnection(.....)

Try
myConn.Open()
mySQLCommand.ExecuteNonQuery()
myConn.Close()
Catch ex As Exception
'code to display error message
Finally
myConn.Close()
End Try

End Using


The reason for still calling Close in the Try and the Finally is that you
want to close your connections as soon as you possibly can. So, if all goes
well, your connection will be closeed at the end of the Try, but if there is
an exception before the Try block's Close call, then the connection will
still be closed in the Finally, which might still be several lines of code
away from the End Using block.

-Scott




Scott M.
  Reply With Quote
Old 11-02-2009, 09:39 PM   #2
Scott M.
 
Posts: n/a
Default Re: How to close connection?

"aspfun via DotNetMonster.com" <u53138@uwe> wrote in message
news:9e8785c960052@uwe...
> Scott M. wrote:
>>>I used code below to close connection, is it right?
>>>

>>[quoted text clipped - 10 lines]
>>>
>>> Does it need " mySQLCommand.Dispose()"?

>>
>>If you are going to dispose the connection, then that will cause it to be
>>closed if needed, so closing and disposing the connection, while not
>>incorrect, is overkill.
>>
>>Also, since your command is tied to an explicit connection, there isn't a
>>need to dispose of the command. Again, it's not wrong, just overkill in
>>this case.
>>
>>You could avoid the dispose calls completely, by declaring your connection
>>in a Using block as in:
>>
>>Using myConn As New SqlConnection(.....)
>>
>> Try
>> myConn.Open()
>> mySQLCommand.ExecuteNonQuery()
>> myConn.Close()
>> Catch ex As Exception
>> 'code to display error message
>> Finally
>> myConn.Close()
>> End Try
>>
>>End Using
>>
>>The reason for still calling Close in the Try and the Finally is that you
>>want to close your connections as soon as you possibly can. So, if all
>>goes
>>well, your connection will be closeed at the end of the Try, but if there
>>is
>>an exception before the Try block's Close call, then the connection will
>>still be closed in the Finally, which might still be several lines of code
>>away from the End Using block.
>>
>>-Scott

>
>
> Thank you, Scott.
> I am confused how to add "using..."
> Before "try...finally", I have code like below:
>
> Dim myDSN As String =
> ConfigurationManager.AppSettings("ConnectionString ")
> Dim myConn As New SqlConnection(myDSN)
> Dim mySQLCommand As New SqlCommand("sUM_updateauthmasters", myConn)
> mySQLCommand.CommandType = CommandType.StoredProcedure
> ...code to add parameters...
>
> If I want to add "using...." block as you suggested, where to add it?
>
> --
> Message posted via DotNetMonster.com
> http://www.dotnetmonster.com/Uwe/For...p-net/200911/1


Using is an alternative way to either declare a new object variable or
indicate the scope that an existing one should be used, so you could do
either of these:

Dim myDSN As String =
ConfigurationManager.AppSettings("ConnectionString ")

'Here the keyword Using replaces the keyword Dim
'so the connection is not only declared, but the scope of its inteneded
'use is also declared. When the End Using statement is reached
'the connection's dispose method will automatically be called.

Using myConn As New SqlConnection(myDSN)
Dim mySQLCommand As New SqlCommand("sUM_updateauthmasters", myConn)
mySQLCommand.CommandType = CommandType.StoredProcedure

Try
myConn.Open()
mySQLCommand.ExecuteNonQuery()
myConn.Close()
Catch ex As Exception
'code to display error message
Finally
myConn.Close()
End Try
End Using

************************************************** **********************

Dim myDSN As String =
ConfigurationManager.AppSettings("ConnectionString ")
Dim myConn As New SqlConnection(myDSN)
Dim mySQLCommand As New SqlCommand("sUM_updateauthmasters", myConn)
mySQLCommand.CommandType = CommandType.StoredProcedure

'Here, the variable was declared normally, but Using is used to
'denote the scope with with the connection will be used.
Using myConn
Try
myConn.Open()
mySQLCommand.ExecuteNonQuery()
myConn.Close()
Catch ex As Exception
'code to display error message
Finally
myConn.Close()
End Try
End Using


-Scott





Scott M.
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: crappy DSL bd Computer Support 0 06-13-2009 03:56 PM
automatic ad-hoc wireless connection? sobriquet Wireless Networking 9 03-06-2009 07:04 PM
Wireless Internet Connection Peter Kennedy Wireless Networking 8 07-02-2005 10:48 PM
connection problem - dial-up and broadband conflicting frankg Computer Support 31 09-26-2004 05:13 PM
New Microsoft Security scare? Peter James Computer Security 42 02-13-2004 11:06 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46