better this way :
Dim objComm As SqlCommand
Try
objComm = New SqlCommand()
objComm.Connection = New SqlConnection("blah blah connectin string")
,,,,blah...blah...
Catch ex As Exception
,,,,blah...blah...
Finally
If objconn IsNot Nothing Then
objconn.Close()
objconn.Dispose()
End If
If objComm IsNot Nothing Then
objComm.Dispose()
End If
End Try
On 5/27/09, Chris Marks <[email protected]> wrote:
>
> I've posted this on a couple forums, but in order to gain a wider
> audience, and possible responses I thought i'd post it here too!
>
> I normally connect to a database as follows:
>
> using (SqlConnection connection = new SqlConnection
> ("connectionString"))
> {
> connection.Open();
> using (SqlCommand command = new SqlComamnd
> ("storedProc",connection))
> {
> command.CommandType = CommandType.StoredProcedure;
> command.Parameters.AddWithValue("@P1",Value);
> using (SqlDataReader reader = command.ExecuteReader())
> {
> if (reader.HasRows)
> {
> reader.Read();
> // rest of code to populate tables, etc here
> }
> }
> }
> }
>
> However, shortly I'm going from typically serving say 200 users, to
> more like 60,000 users, and as such I'd like to ensure that I'm doing
> everything I can to create efficient code. I've been looking at other
> examples, using the "try/catch/finally" methods, but further reading
> supports using "using" more so, due to the fact that it will always
> clean up.
>
> With regard to trapping errors in the commands/connections - should I
> be using a try/catch/finally block within my "using" blocks?
>
> TIA!
>
> Chris
>
> Thanks!
>