Solved

caling Store procdure

Posted on 2008-10-29
9
194 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 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
                    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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

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.…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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