Solved

Scope of SqlCommand object with USING

Posted on 2013-01-02
1
219 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calling stored proc in EDMX 11 23
SSRS ReportViewer report timeout 7 104
Message not shown 5 33
Executing SSIS with a Stored Procedure Permissions 12 26
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

896 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now