Link to home
Start Free TrialLog in
Avatar of VBdotnet2005
VBdotnet2005Flag for United States of America

asked on

caling Store procdure

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()
ASKER CERTIFIED SOLUTION
Avatar of Juan_Barrera
Juan_Barrera
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of VBdotnet2005

ASKER

I am very new with  "Using and end Using" Could you explain a benefit of using it?
I am glad you use that as a sample. I have seen a few use it but not sure why...
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. "

 
Also, is it necessary to use Try Catch block sqlConn.Open() ?
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.
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
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!
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  :)