Solved

Scope of SqlCommand object with USING

Posted on 2013-01-02
1
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 75

Accepted Solution

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

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

734 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