将 nvarchar 值转换为数据类型 int 的列时出现语法错误
我在数据库的 Level 内将 1,2,3,4,5,6,7,8,9 作为 nvarchar 存储。
然后我有一个值为 1,2,3,4,5,6,7,8,9 的下拉列表。当用户做出选择(即 1)时(Level.SelectedValue.ToString)。这通过这样的参数构建了一个 sql 查询:
"Select things From MBA_EOI Where level = 1"
当我运行 select 时,我收到以下错误:
Syntax error converting the nvarchar value '1,2,3,4,5,6,7,8,9' to a column of data type int.
我的印象是我正在处理 Nvarchar 字段并将所选值作为字符串, int 转换来自哪里?
ps 我也尝试过 Level.SelectedItem.ToString
I have 1,2,3,4,5,6,7,8,9 stored as nvarchar inside Level in my db.
I then have a dropdownlist with values 1,2,3,4,5,6,7,8,9. When a user makes a selection (i.e 1) (Level.SelectedValue.ToString). This builds an sql query via a param like this:
"Select things From MBA_EOI Where level = 1"
When I run the select I get the following error:
Syntax error converting the nvarchar value '1,2,3,4,5,6,7,8,9' to a column of data type int.
I was under the impression that I was dealing with an Nvarchar field and the selected value as string, where does the int conversion come in?
p.s I have also tried Level.SelectedItem.ToString
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过包含
level = 1
,您向 SQL 暗示您想要进行数字比较,并且它会尝试将所有列值转换为整数。如果您改用level = '1'
,您将获得不进行转换的文本比较。By including
level = 1
you are implying to SQL that you want a numeric comparison, and it's attempting to cast all the column values to ints. If you usedlevel = '1'
instead, you'd get a text comparison with no conversion.