SqlCommand参数吃+
我有这个:
string a = "a+a";
SqlCommand q = new SqlCommand("SELECT * FROM table WHERE a = @a", conn);
q.Parameters.AddWithValue("@a", a);
但是参数化完全删除了 a
中的 +,留下了 a a
而不是所需的 a+a
。我需要那个+到位;我只是想让它逃脱,而不是删除。
有没有办法让 C# 转义 + 而不是删除它?我正在使用 .NET Framework 2.0,并且没有升级选项。
I have this:
string a = "a+a";
SqlCommand q = new SqlCommand("SELECT * FROM table WHERE a = @a", conn);
q.Parameters.AddWithValue("@a", a);
But the parameterization totally erases the + from a
, leaving me with a a
instead of the desired a+a
. I need that + in place; I just want it escaped, not removed.
Is there a way I can tell C# to escape the + instead of erasing it? I am using .NET Framework 2.0 and don't have the option to upgrade.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
谢谢大家。我不确定这里到底发生了什么,但我最终只是在存储之前将所有 + 符号替换为零。
我想我记得通过查询字符串传输这个变量,但我不记得确切了。如果我这样做了,那么加号可能会被 qs 解析器吃掉,而不是参数化代码。您可能想检查一下。
我没有尝试指定数据类型,因为我很着急,将 + 替换为像 0 这样不会被吃掉的东西是最快的解决方案。
再次感谢所有贡献者。
Thanks everyone. I'm not sure exactly what happened here but I ended up just replacing all + signs with zeros before storing.
I think I remember transferring this variable over the querystring, but I don't remember exactly. If I did, then probably the plus was eaten by the qs parser, not the parameterization code. You may want to check that.
I did not try specifying a datatype because I was in a hurry and replacing the + for something that doesn't get eaten like 0 was the fastest solution.
Thanks again to all contributors.
相反尝试
只是确定这是否是问题所在
instead try
Just make sure if that's the problem
您需要明确设置参数的数据类型
You need to explictly set a datatype for the parameter