如何解决将参数传递到MySqlParameter时,为什么会出现“ SQL语法有错误”的提示?
| 我的代码出现异常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 \'\'MachineSimulator\'\'
var sqlParams = new[]
{
new MySqlParameter(\"@MachineSimulatorDb\",GlobalVariables.MachineSimulatorDb),new MySqlParameter(\"@CAMXMassagesTable\",GlobalVariables.CamxmassagesTable)
};
using (var conn = new MySqlConnection(GlobalVariables.ConnectionString))
{
conn.Open();
using(var command = new MySqlCommand())
{
command.Connection = conn;
command.Parameters.AddRange(sqlParams);
command.CommandText = \"CREATE DATABASE IF NOT EXISTS @MachineSimulatorDb;\";
command.ExecuteNonQuery();
}
}
知道那里有什么问题吗?
谢谢
解决方法
MySQL可能不允许您将参数传递给
create database
。尝试:
command.CommandText = string.Format(\"CREATE DATABASE IF NOT EXISTS `{0}`\",GlobalVariables.MachineSimulatorDb);
如果使用的变量来自usafe来源,则应注意SQL注入的危险。
, 当我使用MySqlParameter时,它正在使用??字符,而不是@字符。这可能是您的问题;例如:
new MySqlParameter(\"?MachineSimulatorDb\");
但是Andomar可能是正确的,因为您不能使用MySqlParameter来提供新数据库的名称。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。