我对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参数:?要么 @
- c#获取客户端IP地址(考虑代理) 2023-02-07
- C#实现的4种常用数据校验方法小结(CRC校验,LRC校验,BCC校验,累加和校验) 2023-05-26
- C#中的composite模式示例详解 2023-06-15
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C#进销存管理系统sqlserver和C# 2023-11-13
- Unity实现游戏卡牌滚动效果 2023-01-16
- C#多线程之线程中止Abort()方法 2023-05-31
- c# – 如何使用Windows任务计划程序执行.Net Core 2.0控制台应用程序? 2023-09-19
- C#图片按比例缩放实例 2022-11-02
- C#实现将网址生成二维码图片方法介绍 2023-06-04