SqlCommand参数吃+

发布于 2024-09-20 00:50:20 字数 339 浏览 6 评论 0原文

我有这个:

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 技术交流群。

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

发布评论

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

评论(3

岁月染过的梦 2024-09-27 00:50:27

谢谢大家。我不确定这里到底发生了什么,但我最终只是在存储之前将所有 + 符号替换为零。

我想我记得通过查询字符串传输这个变量,但我不记得确切了。如果我这样做了,那么加号可能会被 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.

≈。彩虹 2024-09-27 00:50:26

相反尝试

q.Parameters.Add( "@a", SqlDbType.Text ).Value = a;

只是确定这是否是问题所在

instead try

q.Parameters.Add( "@a", SqlDbType.Text ).Value = a;

Just make sure if that's the problem

醉酒的小男人 2024-09-27 00:50:25

您需要明确设置参数的数据类型

You need to explictly set a datatype for the parameter

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