Solved

Scope of SqlCommand object with USING

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

Accepted Solution

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

11 Experts available now in Live!

Get 1:1 Help Now