caling Store procdure

Posted on 2008-10-29
Last Modified: 2012-05-05
is this a correct way to calling a store procedure in Or would you do it differently? Also, what if there are multple param ?

Dim sqlConn As SqlConnection = New SqlConnection(_connectionString)

Dim sqlComm As New SqlCommand("nameofstoreprocedure", sqlConn)
sqlComm.CommandType = Data.CommandType.StoredProcedure

Dim paramUser As SqlParameter = New SqlParameter("@myparam", Data.SqlDbType.VarChar, 3)
Question by:VBdotnet2005
  • 5
  • 4
LVL 20

Accepted Solution

Juan_Barrera earned 125 total points
ID: 22835211
Hi, this is the way I'd do it:

   Using sqlConn As New SqlConnection(_connectionString) 'Using statement to properly dispose the object
                Using sqlComm As New SqlCommand("nameofstoreprocedure", sqlConn)  'Using statement to properly dispose the object
                    sqlComm.CommandType = CommandType.StoredProcedure
                    sqlComm.Parameters.AddWithValue("@myparam", "myvalue")
                    sqlConn.Open() 'Open the connection as late as possible
                    sqlConn.Close() 'And close it as soon as possible. 
                End Using
            End Using               'However, this closing the Using statement will close the connection anyway

Open in new window


Author Comment

ID: 22837908
I am very new with  "Using and end Using" Could you explain a benefit of using it?

Author Comment

ID: 22837912
I am glad you use that as a sample. I have seen a few use it but not sure why...
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 20

Expert Comment

ID: 22837914
It's a not so simple topic, you should read the following link:
In you specific case, have a look at the fllowing paragraph:
"Sometimes your code requires an unmanaged resource, such as a file handle, a COM wrapper, or a SQL connection. A Using block guarantees the disposal of one or more such resources when your code is finished with them. This makes them available for other code to use.
Managed resources are disposed of by the .NET Framework garbage collector (GC) without any extra coding on your part. You do not need a Using block for managed resources. "


Author Comment

ID: 22837919
Also, is it necessary to use Try Catch block sqlConn.Open() ?
LVL 20

Expert Comment

ID: 22837928
No, the using statement will take care of errors, unless you want to execute a piece of code if a particular exception happens. Then, you'll need a Try / Catch to do it.

Author Comment

ID: 22837943
according to the link you gave, it is really much like Try...Finally ... like ?

If you need finer control over the acquisition of the resources, or you need additional code in the Finally block, you can rewrite the Using block as a Try...Finally construction. The following example shows skeleton Try and Using constructions that are equivalent in the acquisition and disposal of resource.
Copy Code

            Using resource As New resourceType
    ' Insert code to work with resource.
End Using
Dim resource As New resourceType
    ' Insert code to work with resource.
Catch ex As Exception
    ' Insert code to process exception.
    ' Insert code to do additional processing before disposing of resource.
End Try
LVL 20

Expert Comment

ID: 22837953
Yes, it is like Try / Catch / Finally, except that, by using it, you make absolutely sure that an unmanaged resource, like your SqlConnection , is properly disposed.
By using only Try / Catch blocks,if written uncorrectly, there is that risk.
Besides, is easier and cleaner!

Author Comment

ID: 22837994
I just read about this Using statement too  That makes alot more sense.... now  I thank you very much for your quick reply  :)

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Initial get page response times and IIS8 15 61
"Emulate" TAB key when press Enter Key 3 68
Where is this file? 3 33
locate sql commands in C# visual studio Project 6 37
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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

735 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