C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

A common function in VB.NET to execute any stored procedure in SQL server 2000


Posted Date: 05 Jul 2004    Resource Type: Articles    Category: .NET Framework
Author: Nikhil GuptaMember Level: Bronze    
Rating: 1 out of 5Points: 7




Public Function QueryExcuter(ByVal iParamCount As Integer, _
ByVal arrInputValues() As String, _
ByRef arrOutputParams() As String, _
ByVal sSPName As String, _
ByVal sDSname As String) As DataSet

Dim objCon As SqlConnection
Dim objAdapter As SqlDataAdapter
Dim cmdCommand As New SqlCommand
Dim iCount As Integer
Dim objDataSet As DataSet
Dim objParam As SqlParameter
Dim sconnection As String

Try
sconnection = "data source =;initial catalog = ; user ID=; password = "
objAdapter = New SqlDataAdapter
objCon = New SqlConnection(sconnection)
objCon.Open()
objAdapter.SelectCommand = cmdCommand
objAdapter.SelectCommand.Connection = objCon
objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
objAdapter.SelectCommand.CommandText = sSPName

Try
SqlCommandBuilder.DeriveParameters(objAdapter.SelectCommand)

For iCount = 1 To iParamCount
If (objAdapter.SelectCommand.Parameters(iCount).Direction = _
ParameterDirection.Input) Or _
(objAdapter.SelectCommand.Parameters(iCount).Direction = _
ParameterDirection.InputOutput) Then

objAdapter.SelectCommand.Parameters(iCount).Value = arrInputValues(iCount - 1)
End If
Next

objDataSet = New DataSet(sDSname)
objAdapter.Fill(objDataSet)
iCount = 0
For Each objParam In objAdapter.SelectCommand.Parameters
If (objParam.Direction = ParameterDirection.Output) Or _
(objParam.Direction = ParameterDirection.InputOutput) Then
ReDim Preserve arrOutputParams(iCount)
arrOutputParams(iCount) = objParam.Value
iCount += 1
End If
objParam = New SqlParameter
Next


Catch ex As SqlException
objCon.Close()
Throw ex
End Try
Catch ex As Exception
objCon.Close()
Throw ex
End Try
objCon.Close()
Return objDataSet

End Function


Please note that I have only taken care to close the connection in the TRY CATCH blocks. If necessary do any cleaning up u might need !!
Next I will post a common function to execute SPs which do not return any data. A single function that will execute all your UPDATE, DELETE and INSERT SPs !!!




Responses

Author: Nikhil Gupta    15 Jul 2004Member Level: Bronze   Points : 0
hi All !
thanks for the great response to my code. but can you please Submit your Feedback here on the site instead of mailing them to me ? And Sridhar I tried mailing you but it bounced back, do u have any other mail id ?



Author: challa laxman    04 Aug 2004Member Level: Bronze   Points : 0
This article is very good, I need this sample.
Pls send the sample examples how to use this procedure.


Author: solvers    09 Oct 2004Member Level: Bronze   Points : 0
I have no words to appreciate this code. The code clearly depicts the caliber you possess. This will be highly beneficial if get integrated with the coder''s code.If you can post an example to illustrate this code and how to call this function, then i will be highly beneficial to you.


Author: Nikhil Gupta    09 Oct 2004Member Level: Bronze   Points : 0
Allright people !

Thanks a lot for your mails. Within past one week I received like 100 mails in my inbox !!
But its much better to see ur comments on the site and so can u please post them here only ?
Now what do you get it u post ur comment - this funtction is just one part of a complete class I wrote from scratch that takes care of all the SQL server processing needs ! Write ur comment here and then drop in a mail to me and I shall be more than happy to pass on the code :-). Right now I am in New York but I might have to go back to India in January :-(. If any of u guys have anything that can help me with staying in US I'll be grateful !!
Thanks !!


Author: Lisa C    20 Oct 2004Member Level: Bronze   Points : 0
This is a good example. Thanks!!


Author: Shrikanth    23 Dec 2004Member Level: Bronze   Points : 0
Hi
It's Very Usefull Example.
Keep it Up.



Author: saurav gupta    05 Mar 2005Member Level: Bronze   Points : 0
Like all generic pieces of code (which do have their own flaws and drawbacks) this one no exception. It's generic - and generic comes with a price all have to pay.

Man - this code will make unnecessary server trips to generate the parameter information. This is more expensive than calling the proc directly passing in the parameter values.

Though a good effort, but never use this in a real production database with thousands of consumers hitting the db server every minute. This piece of code is really going to double the workload on the server and performance hit is inevitable (make use of the profiler to see for yourself).



Author: Brundaban    21 Mar 2005Member Level: Silver   Points : 0
u can save from lengthy coding ,can u pls PUBLISH the function which will execute all INSERT ,UPDATE SPS.that will complete this issue i think.


Author: venkatesh    01 Apr 2005Member Level: Bronze   Points : 0
Hi, Please can yoou send me the Wokring version of generic stored procedure using C#. I would be gratefull to you. I need this urgently. I hope you will be keeping track of feedbacks on this artcile. I will be waiting for your reply.
Thanks


Author: venkatesh    07 Apr 2005Member Level: Bronze   Points : 0
Hi,
This code will save lot of time. If possible can you send the working version of code in C#. Please send the code to v_bhupathi@yahoo.com. Expecting a reply from you.Hope you would be keeping track of this requests.
Thank you


Author: bayla nussbaum    04 May 2005Member Level: Bronze   Points : 0
can you please post the other part of the code?


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Accessing Multiple ResultSets using DataReader
Previous Resource: Clear the Forms control which either in a Group box or any container control
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use