Solved

Scope of SqlCommand object with USING

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…

732 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