C# 为什么我会得到;您的SQL语法中有一个错误";当我将参数传递到MySqlParameter时?
我的代码出现以下异常C# 为什么我会得到;您的SQL语法中有一个错误";当我将参数传递到MySqlParameter时?,c#,.net,mysql,sql,C#,.net,Mysql,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[] {
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可能不允许您将参数传递给
创建数据库
。尝试:
command.CommandText = string.Format("CREATE DATABASE IF NOT EXISTS `{0}`",
GlobalVariables.MachineSimulatorDb);
如果您使用的变量来自usafe源,您应该意识到SQL注入的危险。MySQL可能不允许您将参数传递给
创建数据库。尝试:
command.CommandText = string.Format("CREATE DATABASE IF NOT EXISTS `{0}`",
GlobalVariables.MachineSimulatorDb);
如果您使用的变量来自usafe源,您应该意识到SQL注入的危险。当我使用MySqlParameter时,它使用的是一个?字符,而不是@字符。这可能是你的问题;例如:
new MySqlParameter("?MachineSimulatorDb");
但是Andomar可能是正确的,您不能使用MySqlParameter来提供新数据库的名称。当我使用MySqlParameter时,它使用的是?字符,而不是@字符。这可能是你的问题;例如:
new MySqlParameter("?MachineSimulatorDb");
但Andomar可能是正确的,您不能使用MySqlParameter来输入新数据库的名称。“,如果我尝试使用@MachineSimulatorDb;”;我也有同样的例外。所以这不是问题。@Night Walker:更有可能的是,use
也不允许使用参数。一般情况下,值允许使用参数,而不是指定数据库对象。因此,其中id=@MyParameter
将起作用,但从@MyTable
中选择*将不起作用。”,如果我尝试使用@MachineSimulatorDb;”;我也有同样的例外。所以这不是问题。@Night Walker:更有可能的是,use
也不允许使用参数。一般情况下,值允许使用参数,而不是指定数据库对象。因此,其中id=@MyParameter
将起作用,但从@MyTable
中选择*将不起作用。