我对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参数:?要么 @


基础教程推荐
- sql server和c#中的输出参数问题 2023-11-10
- C#中DateTime的时间加减法操作小结 2023-06-22
- C# winform跨线程操作控件的实现 2023-06-14
- c#预处理指令分析 2023-03-09
- WPF实现调用本机摄像头的示例代码 2023-06-27
- C#异步迭代IAsyncEnumerable应用实现 2023-04-21
- C#使用Selenium+PhantomJS抓取数据 2022-11-03
- .Net Core快速创建Windows服务 2023-09-28
- C#连接SQL Server的实现方法 2023-01-06
- C# 判断时间段是否相交的实现方法 2022-11-22