i am getting an error Argument 2 may not be passed with ref keyword while using ado.net

前提是你 提交于 2020-01-04 04:38:08

问题


int? t = 0;
cmd.Parameters.AddWithValue("@Res", ref t);

I get an error in the second line:

argument 2 may not be passed with ref keyword.


回答1:


You can only pass an argument by reference with ref if the parameter is a ref parameter as well. AddWithValue doesn't have any ref parameters, so you can't use it that way. Note that you have to specify ref when calling a method if a parameter has the ref modifier. So:

public void WithRef(ref int x) {}
public void WithoutRef(int x) {}

...


int y = 0;
// Valid
WithRef(ref y);
WithoutRef(y);
// Invalid
WithRef(y);
WithoutRef(ref y);

Basically, there's no way of telling an ADO.NET command parameter to track the current value of a variable - after all, that variable could be a local variable which will be "gone" by the time you use the command.

Instead, just compute the right value and then set it for the parameter value.



来源:https://stackoverflow.com/questions/41777751/i-am-getting-an-error-argument-2-may-not-be-passed-with-ref-keyword-while-using

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