Solved

caling Store procdure

Posted on 2008-10-29
9
189 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
Comment Utility
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
Comment Utility
I am very new with  "Using and end Using" Could you explain a benefit of using it?
0
 

Author Comment

by:VBdotnet2005
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

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

Expert Comment

by:Juan_Barrera
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
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…

763 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

10 Experts available now in Live!

Get 1:1 Help Now