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();
}
}
These answers say both @
or ?
work fine.
Even cmd.Parameters.AddWithValue("@name", name);
seems to work (note the @
in the name).
Why all of them work fine with MySQL ?
Is there a difference between them ?
Which one is the proper way to use with MySQL ?
Thanks for any help I'll get.