我收到错误,使用ado.net时可能无法使用ref关键字传递参数2 [英] i am getting an error Argument 2 may not be passed with ref keyword while using ado.net

查看:309
本文介绍了我收到错误,使用ado.net时可能无法使用ref关键字传递参数2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

第二行出现错误:


参数2不能与ref关键字一起传递。

argument 2 may not be passed with ref keyword.


推荐答案

您只能使用 ref ,如果参数也是 ref 参数。 AddWithValue 没有任何 ref 参数,因此您不能以这种方式使用它。请注意,如果参数具有 ref ,则在调用方法时必须指定 ref 修饰符。因此:

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);

基本上,无法告诉ADO.NET命令参数来跟踪变量的当前值-毕竟,该变量可能是局部变量,在您使用命令时该变量将消失。

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.

这篇关于我收到错误,使用ado.net时可能无法使用ref关键字传递参数2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆