MySQL 前导空格与 C#

发布于 2024-12-06 00:23:36 字数 604 浏览 0 评论 0原文

当我更新 MySQL 数据库中的字段时,它总是在值中添加空格。 我尝试使用修剪命令和替换命令删除空格。他们都没有工作。所以我希望它不是一个空格而是一些模糊的 ASCII 字符。这些是我使用的命令:

this.foo = result.GetValue(0).ToString().Trim();
this.bar = result.GetValue(0).ToString().Replace(" ","");

它更新的字段是 VARCHAR(xx)。这是我的 MySQL 更新命令:

            MySqlCommand cmd = new MySqlCommand("UPDATE " + table + " SET " + new_field + " =' " + new_value+ "' WHERE " + field+ "= " + value + "",this.con);

this.con 是我与 MySQL 数据库的连接。

仅供参考:我在 Visual Studio 2008 中使用 .NET 3.5CF 和 mysql.data.cf DLL。

有人可以帮我解决这个问题吗?这让我抓狂。

When I update a field in my MySQL database, it always adds a whitespace to the value.
I tried to remove the whitespace with the trim-command and the replace-command. Neither of them worked. So I expect that it isn't a whitespace but some vague ASCII character. These are the commands I used:

this.foo = result.GetValue(0).ToString().Trim();
this.bar = result.GetValue(0).ToString().Replace(" ","");

The field it updates is a VARCHAR(xx). This is my MySQL update command:

            MySqlCommand cmd = new MySqlCommand("UPDATE " + table + " SET " + new_field + " =' " + new_value+ "' WHERE " + field+ "= " + value + "",this.con);

this.con is my connection to the MySQL database.

FYI: I use .NET 3.5CF with a mysql.data.cf DLL in Visual Studio 2008.

Could someone help me out with this problem? It's driving me nuts.

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

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

发布评论

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

评论(2

追星践月 2024-12-13 00:23:36

是的,SQL 中有一个前导空格:

"UPDATE " + table + " SET " + new_field + " =' " + new_value+ "'

请注意“=”后面的那一位 - 您先有一个引号,然后是一个空格,然后是 new_value

但是,您不应该首先将值直接放入 SQL 中 - 您应该使用参数化 SQL 语句...目前您已经面临 SQL 注入攻击,以及潜在的问题 诚实的值带引号。

您应该在此处对new_valuevalue使用参数化SQL...我假设field 和 table 来自更“可信”的来源?

Well yes, you've got a leading space in the SQL:

"UPDATE " + table + " SET " + new_field + " =' " + new_value+ "'

Note the bit straight after "=" - you've got a quote, then a space, then new_value.

However, you shouldn't be putting the values in the SQL directly in the first place - you should be using parameterized SQL statements... currently you've got a SQL injection attack waiting to happen, as well as potential problems for honest values with quotes in.

You should use parameterized SQL for both new_value and value here... I'm assuming that field and table come from more "trusted" sources?

抹茶夏天i‖ 2024-12-13 00:23:36

这似乎有一个空格,其中 * 是

" ='*" + new_value

This appears to have a space where the * is

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