SqlParameter 构造函数与对象初始值设定项的问题

发布于 2024-12-08 14:36:57 字数 407 浏览 2 评论 0原文

给出以下代码行:

cmd.Parameters.Add(new SqlParameter("@displayId", SqlDbType.NVarChar).Value = customer.DisplayID);

我收到以下错误: SqlParameterCollection 仅接受非空 SqlParameter 类型对象,而不接受 String 对象。

但是,重写它以使用对象初始化:

cmd.Parameters.Add(new SqlParameter("@displayId", SqlDbType.NVarChar) { Value = customer.DisplayID });

效果很好。有任何关于为什么会发生这种情况的指示吗?

Given the following line of code:

cmd.Parameters.Add(new SqlParameter("@displayId", SqlDbType.NVarChar).Value = customer.DisplayID);

I receive the following error:
The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects.

However, rewriting it to use object intialization:

cmd.Parameters.Add(new SqlParameter("@displayId", SqlDbType.NVarChar) { Value = customer.DisplayID });

works just fine. Any pointer on why this is occuring?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

预谋 2024-12-15 14:36:57

问题是右括号放错了位置:

cmd.Parameters.Add(new SqlParameter("@displayId", SqlDbType.NVarChar)).Value = customer.DisplayId;

请注意,.Value 之前有 2 个右括号。正如您最初输入的那样,您正在执行 cmd.Parameters.Add(...);,其中 ...

new SqlParameter("@displayId" ,SqlDbType.NVarChar).Value = customer.DisplayId

并且计算结果为 customer.DisplayId,因此有关它的消息不接受 string 类型。

更简洁地添加参数

cmd.Parameters.AddWithValue("@displayId", customer.DisplayId);

此外,您还可以使用As to Why new SqlParameter("@displayId", SqlDbType.NVarChar).Value = customer.DisplayId returns customer.DisplayId ,考虑赋值运算符返回作为结果分配的值,在本例中为 customer.DisplayId。这就是为什么您可以一次为多个变量赋值:

int i, j, k;
i = j = k = 42;

The problem is a misplaced closing parenthesis:

cmd.Parameters.Add(new SqlParameter("@displayId", SqlDbType.NVarChar)).Value = customer.DisplayId;

Note that there are 2 closing parentheses before .Value. As you originally entered it, you are doing cmd.Parameters.Add(...); where the ... is

new SqlParameter("@displayId", SqlDbType.NVarChar).Value = customer.DisplayId

and that evaluates to customer.DisplayId, hence the message about it not accepting string types.

Also, you can add the parameter more succinctly with

cmd.Parameters.AddWithValue("@displayId", customer.DisplayId);

As to why new SqlParameter("@displayId", SqlDbType.NVarChar).Value = customer.DisplayId returns customer.DisplayId, consider that the assignment operator returns the value being assigned as its result, and in this case that would be customer.DisplayId. This is why you can assign a value to several variables at once:

int i, j, k;
i = j = k = 42;
舂唻埖巳落 2024-12-15 14:36:57
cmd.Parameters.Add(new SqlParameter("@displayId", SqlDbType.NVarChar).Value = customer.DisplayID);

和现在一样

var customerDisplayId = customer.DisplayID;
new SqlParameter("@displayId", SqlDbType.NVarChar).Value = customerDisplayId;
cmd.Parameters.Add(customerDisplayId);

你明白为什么编译器会抱怨吗?

cmd.Parameters.Add(new SqlParameter("@displayId", SqlDbType.NVarChar).Value = customer.DisplayID);

is the same as

var customerDisplayId = customer.DisplayID;
new SqlParameter("@displayId", SqlDbType.NVarChar).Value = customerDisplayId;
cmd.Parameters.Add(customerDisplayId);

now do you see why the compiler is complaining?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文