Solved

Scope of SqlCommand object with USING

Posted on 2013-01-02
1
220 Views
Last Modified: 2013-01-06
Hi all.

I am familiar with this code block below...

Using conn As New SqlConnection(GetConnectionString())
            Using cmd As New SqlCommand(strCmd, conn)
                conn.Open()
                Try
                    Using dr As SqlDataReader = cmd.ExecuteReader()
                   ' do the work
                    End Using
                Catch ex As Exception

                End Try
            conn.Close()
            End Using
        End Using

...and am happy with this, however does the command object stay wrapped in the 'using block' and cleaned up and disposed of just the same if I did this...

Using conn As New SqlConnection(GetConnectionString())
            Using cmd As SqlCommand = GetMyCommand(WithSpecialParametersEtc)
                cmd.connection = conn
                conn.Open()
                Try
                    Using dr As SqlDataReader = cmd.ExecuteReader()
                   ' do the work
                    End Using
                Catch ex As Exception

                End Try
            conn.Close()
            End Using
        End Using

function GetMyCommand(WithSpecialParametersEtc) as SqlCommand
     dim cmd as new SqlCommand
     ' Set up the command text and parameters here
     return cmd
end function

I hope this makes sense and thanks for your time with it.
0
Comment
Question by:MonCapitan
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38737079
Yes.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp day pilot 3 18
ASP.NET gridview select textbox on focus 2 33
List<PaisEntity> - show some 1 24
i have to take the screenshot of command prompt? how to do this? 1 35
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question