Omit named parameters from OleDbCommand [closed]

最后都变了- 提交于 2020-01-14 07:11:12

问题


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:

  1. Define a connection and Open it.

  2. Define a command object

  3. Add parameters to the command object.

  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!