问题
I have a Simple stored procedure which takes 6 paramters. Lets say they are @a, @b, @c, @d, @e, @f
and they are all optional with the default value of null
.
I need to know how to call the stored procedure with only some of these parameters. I know the general steps:
Define a connection and Open it.
Define a command object
Add parameters to the command object.
Execute the command.
Right now for each of @a, @b, @c, @d, @e, @f
I use Parameters.Add()
to add that parameter.
Question is: what if I want to call the stored procedure with just @a
and @f
, do I need to Add()
the other parameters also? Or if I add just 2 parameters how will the stored procedure know which parameters I supplied?
回答1:
Question is :What if I want to call the stored procedure with just @a and @f,
As long as other are optional SP will be executed without any errors.
In this case do I need to Add other parameters also (b,c,d,e) anyway?
No, you don't.
If I add just 2 parameters how stored procedure knows the order?
Order is not important here. The parameter name is. It has to match the one from SP declaration.
回答2:
since you said all the parameters are optional (allow null); so even if you call the SP without any parameters it should run fine.
回答3:
If you define your parameters as nullable in your proc your will not have any problems by running it.
As other write the important part is that you name your parameters as they are named in the procedure
来源:https://stackoverflow.com/questions/17653626/omit-named-parameters-from-oledbcommand