VBdotnet2005
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(_connectionS tring)
sqlConn.Open()
Dim sqlComm As New SqlCommand("nameofstorepro cedure", sqlConn)
sqlComm.CommandType = Data.CommandType.StoredPro cedure
Dim paramUser As SqlParameter = New SqlParameter("@myparam", Data.SqlDbType.VarChar, 3)
sqlComm.Parameters.Add(par amUser)
sqlComm.ExecuteNonQuery()
sqlConn.Close()
Dim sqlConn As SqlConnection = New SqlConnection(_connectionS
sqlConn.Open()
Dim sqlComm As New SqlCommand("nameofstorepro
sqlComm.CommandType = Data.CommandType.StoredPro
Dim paramUser As SqlParameter = New SqlParameter("@myparam", Data.SqlDbType.VarChar, 3)
sqlComm.Parameters.Add(par
sqlComm.ExecuteNonQuery()
sqlConn.Close()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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/libr ary/htd05w hh(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. "
http://msdn.microsoft.com/
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. "
ASKER
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.
ASKER
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
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!
By using only Try / Catch blocks,if written uncorrectly, there is that risk.
Besides, is easier and cleaner!
ASKER
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 :)
ASKER