execute a stored procedure from c#

ace333

Member
Joined
Jul 19, 2005
Messages
22
I want to execute a stored procedure from where the name of the stored procedure and the parameters are passed to the method in an array, the name of the stored procedure will always be at array[0] and the remaining parameters will follow... However the number of parameters can vary from 1 to many, any ideas guys (and girls) !
 
ace333 said:
I want to execute a stored procedure from where the name of the stored procedure and the parameters are passed to the method in an array, the name of the stored procedure will always be at array[0] and the remaining parameters will follow... However the number of parameters can vary from 1 to many, any ideas guys (and girls) !

What database backend are you using, because it could make a difference in how you execute the code?

You will need to use a command object, either odbc or sql and then add a parameter and set its value for each passed in parameter, as well as its type and size. You will therefore also need to pass in the parameter name and parameter type (int, varchar, bit, etc), as well as the size expected (i.e. 50, 4, etc), along with the parameter value and sp name (you will want to use something other than a single array I think, otherwise you will have to do some kind of parsing on the backend, or use a multidimensional array). In this manner you could set it up based on the number of records in the array. If you are using an ODBC you will also need this number to generate the (?, ?, ?, ..., ?) string for the number of parameters. Also remember that if it is an ODBC you have to specifically set the return parameter on all sp execution, whereas with sql this is not necessary. When generating the parameters you will need to be wary of the type that the parameter is supposed to be, and set it accordingly (which also brings to mind the size variable). You will also need to take into account null values and how you will handle them, because what if an sp requires 10 values but youve only passed in 7 and the other three are supposed to just be null?

Good Luck.
 
Back
Top