我的代码出现异常
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注入的危险。
关于c# - 将参数传递到MySqlParameter时,为什么会出现“SQL语法有错误”的提示?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6396029/