2012-06-20 44 views
3

我得到:C#MySQL的语法错误

您的SQL语法错误;检查对应于你的MySQL服务器 版本正确的语法使用近'谱” WHERE规范ID = 42' 在行1

虽然运行该代码的手册:

public System.Drawing.Image GetImage(int index) 
{ 
using (MySqlCommand command = connection.CreateCommand()) 
{ 
    //command.CommandText = "SELECT imageObj FROM spectra WHERE specId=42"; <== Works OK! 

    command.CommandText = "SELECT imageObj FROM @tname WHERE [email protected]"; 
    command.Parameters.AddWithValue("@index", index); 
    command.Parameters.AddWithValue("@tname", "spectra"); 

    using (MySqlDataReader reader = command.ExecuteReader()) 
    { 
    if (reader.Read()) 
    { 
    return (System.Drawing.Image)Serial.ByteArrayToObject((byte[])reader[0]); 
    } 
    } 
} 
return null; 
} 

我觉得问题是光谱附近的报价。我怎样才能删除它们?

+0

请记住,你不能投了'byte'到'的byte []'... –

+0

无关。功能正常工作时的语法就可以了。 – Igor

回答

4

不能用参数替换表名。可悲的是,这只是不被支持。只有WHERE子句中的参数值可以用这种方式替换。

您必须自己进行替换,而不是依靠MySqlCommand对象。像这样的东西应该工作:

string tableName = "spectra"; 
command.CommandText = 
    String.Format("SELECT imageObj FROM {0} WHERE [email protected]", tableName); 
command.Parameters.AddWithValue("@index", index); 
+0

现在正常工作..感谢您的快速响应。 – Igor