gpt4 book ai didi

c# - 在 mysql 上使用带有参数的 Database.ExecuteSqlCommand 的正确语法是什么?

转载 作者:行者123 更新时间:2023-11-29 02:45:38 26 4
gpt4 key购买 nike

执行mysql存储过程

  var affectedRows = 
context.Database.ExecuteSqlCommand("exec UpdateStatus @p_customerId, @p_id",
new MySqlParameter("p_customerId", "006"),
new MySqlParameter("p_id", 9));

获取MySqlException

An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in EntityFramework.dll

Additional information: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'exec UpdateStatus '006', 9' at line 1

当我尝试用 Update 语句替换存储过程名称时,它起作用了。但我想改为调用存储过程。

我尝试使用 Entity Framework 5 和 6 同样的错误

最佳答案

从命令调用存储过程的 MySQL 语法是“CALL”。此外,参数名称前面应该有“@”。

因此您需要将代码更改如下。

var affectedRows = 
context.Database.ExecuteSqlCommand("CALL UpdateStatus (@p_customerId, @p_id)",
new MySqlParameter("@p_customerId", "006"),
new MySqlParameter("@p_id", 9));

关于c# - 在 mysql 上使用带有参数的 Database.ExecuteSqlCommand 的正确语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42678390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com