C#MySQL参数:?或者 @ [英] C# MySQL Parameters : ? or @
本文介绍了C#MySQL参数:?或者 @的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对MySQL参数感到困惑.
I'm kind of confused with the MySQL parameters.
我的代码的以下两个部分都可以正常工作.第一个使用@
的参数:
Both of the following parts of my code work fine. The first one uses parameters with @
:
const string query = "UPDATE `items` SET `name` = @name, `price` = @price WHERE `id` = @id";
try
{
using (MySqlCommand cmd = new MySqlCommand(query, Database.MyConnection))
{
cmd.Parameters.AddWithValue("name", name);
cmd.Parameters.AddWithValue("price", price);
cmd.Parameters.AddWithValue("id", id);
cmd.ExecuteNonQuery();
}
}
第二个将参数与?
一起使用:
Second uses parameters with ?
:
const string query = "UPDATE `items` SET `name` = ?name, `price` = ?price WHERE `id` = ?id";
try
{
using (MySqlCommand cmd = new MySqlCommand(query, Database.MyConnection))
{
cmd.Parameters.AddWithValue("name", name);
cmd.Parameters.AddWithValue("price", price);
cmd.Parameters.AddWithValue("id", id);
cmd.ExecuteNonQuery();
}
}
这些答案都说@
或?
工作正常.
甚至cmd.Parameters.AddWithValue("@name", name);
似乎都可以工作(请注意名称中的@
).
These answers say both @
or ?
work fine.
Even cmd.Parameters.AddWithValue("@name", name);
seems to work (note the @
in the name).
-
为什么它们都可以在MySQL上正常工作?
Why all of them work fine with MySQL ?
它们之间有区别吗?
哪种是与MySQL一起使用的正确方法?
Which one is the proper way to use with MySQL ?
感谢我的帮助.
推荐答案
查看全文