Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

caling Store procdure

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



Dim sqlConn As SqlConnection = New SqlConnection(_connectionString)
sqlConn.Open()

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

Dim paramUser As SqlParameter = New SqlParameter("@myparam", Data.SqlDbType.VarChar, 3)
sqlComm.Parameters.Add(paramUser)
sqlComm.ExecuteNonQuery()
sqlConn.Close()
0
Comment
Question by:VBdotnet2005
[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
  • 5
  • 4
9 Comments
 
LVL 20

Accepted Solution

by:
Juan_Barrera earned 500 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
                    sqlComm.ExecuteNonQuery()
                    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

0
 

Author Comment

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

Author Comment

by:VBdotnet2005
ID: 22837912
I am glad you use that as a sample. I have seen a few use it but not sure why...
0
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

by:Juan_Barrera
ID: 22837914
It's a not so simple topic, you should read the following link:
http://msdn.microsoft.com/en-us/library/htd05whh(VS.80).aspx
 
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. "

 
0
 

Author Comment

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

Expert Comment

by:Juan_Barrera
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.
0
 

Author Comment

by:VBdotnet2005
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
' THE FOLLOWING TRY CONSTRUCTION IS EQUIVALENT TO THE USING BLOCK
Dim resource As New resourceType
Try
    ' Insert code to work with resource.
Catch ex As Exception
    ' Insert code to process exception.
Finally
    ' Insert code to do additional processing before disposing of resource.
    resource.Dispose()
End Try
0
 
LVL 20

Expert Comment

by:Juan_Barrera
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!
0
 

Author Comment

by:VBdotnet2005
ID: 22837994
I just read about this Using statement too  http://www.pluralsight.com/community/blogs/fritz/archive/2005/04/28/7834.aspx  That makes alot more sense.... now  I thank you very much for your quick reply  :)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

721 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