简体   繁体   English

C#MySQL参数:?要么 @

[英]C# MySQL Parameters : ? or @

I'm kind of confused with the MySQL parameters. 我对MySQL参数感到困惑。

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); 甚至cmd.Parameters.AddWithValue("@name", name); seems to work (note the @ in the name). 似乎工作(请注意名称中的@ )。

  1. Why all of them work fine with MySQL ? 为什么所有这些都适用于MySQL?

  2. Is there a difference between them ? 它们之间有区别吗?

  3. Which one is the proper way to use with MySQL ? 哪一个是与MySQL一起使用的正确方法?

Thanks for any help I'll get. 感谢您的帮助。

From the documentation : 文档

Prior versions of the provider used the '@' symbol to mark parameters in SQL. 提供程序的早期版本使用“@”符号标记SQL中的参数。 This is incompatible with MySQL user variables, so the provider now uses the '?' 这与MySQL用户变量不兼容,因此提供程序现在使用'?' symbol to locate parameters in SQL. 用于在SQL中查找参数的符号。 To support older code, you can set 'old syntax=yes' on your connection string. 要支持旧代码,可以在连接字符串上设置“old syntax = yes”。 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. 如果您这样做,请注意,如果您未能定义要在SQL中使用的参数,则不会抛出异常。

It appears this has changed again 看来这又改变了

From the updated documentation Connector Options 从更新的文档连接器选项

Old Syntax, OldSyntax, Use Old Syntax, UseOldSyntax - This option was deprecated in Connector/Net 5.2.2. 旧语法,OldSyntax,使用旧语法,UseOldSyntax - Connector / Net 5.2.2中不推荐使用此选项。 All code should now be written using the '@' symbol as the parameter marker. 现在应该使用'@'符号作为参数标记来编写所有代码。

Also from Creating a Connector .Net 也来自Creating a Connector .Net

Using the '@' symbol for parameters is now the preferred approach, although the old pattern of using '?' 使用参数的'@'符号现在是首选方法,尽管使用'?'的旧模式 is still supported. 仍然受支持。 To avoid conflicts when using the '@' symbol in combination with user variables, see the Allow User Variables connection string option in Chapter 7, Connector/Net Connection String Options Reference. 要在将“@”符号与用户变量结合使用时避免冲突,请参阅第7章“连接器/网络连接字符串选项参考”中的“允许用户变量连接字符串”选项。 The Old Syntax connection string option has now been deprecated. 现在不推荐使用旧语法连接字符串选项。

@ and ? @和? are both symbol to mark parameters in MySQL, you can even used it both like... 都是在MySQL中标记参数的符号,你甚至可以像...一样使用它

command.CommandText = @"UPDATE sellinginventorytable SET cqty=cqty+@aqty,`date`=CURRENT_TIMESTAMP(),`user`=@user WHERE cid=?cid;
                        INSERT INTO internaltranstable SET `type`='INV.ADJ.', `cid`=?cid, `cqty`=@aqty, `date`=CURRENT_TIMESTAMP(), `notes`='STORE INVENTORY ADJUSTMENT', `area`='COUNTER', `sender`=@user, `receiver`=@user;";
command.Parameters.AddWithValue("?cid", row.Cells["barcode"].Value);
command.Parameters.AddWithValue("@aqty", row.Cells["aqty"].Value);
command.Parameters.AddWithValue("@user", Properties.Settings.Default.user.ToUpper());
command.ExecuteNonQuery();
command.Parameters.Clear();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM