C#MySQL参数:?要么 @

我对MySQL参数感到困惑.我的代码的以下两部分都可以正常工作.第一个使用@的参数:const string query = UPDATE `items` SET `name` = @name, `price` = @price WHERE `id` = @id;try{using (MySqlCommand cmd =...

我对MySQL参数感到困惑.

我的代码的以下两部分都可以正常工作.第一个使用@的参数:

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();
    }
}

第二个使用参数? :

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();
    }
}

These answers说@或者?工作得很好.
甚至cmd.Parameters.AddWithValue(“@ name”,name);似乎工作(请注意名称中的@).

>为什么所有这些都适用于MySQL?
>他们之间有区别吗?
>哪一种是与MySQL一起使用的正确方法?

感谢您的帮助.

解决方法:

从documentation:

Prior versions of the provider used the ‘@’ symbol to mark parameters in SQL. This is incompatible with MySQL user variables, so the provider now uses the ‘?’ symbol to locate parameters in SQL. To support older code, you can set ‘old syntax=yes’ on your connection string. If you do this, please be aware that an exception will not be throw if you fail to define a parameter that you intended to use in your SQL.

本文标题为:C#MySQL参数:?要么 @

基础教程推荐