Solved

caling Store procdure

Posted on 2008-10-29
9
190 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

912 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

20 Experts available now in Live!

Get 1:1 Help Now