簡體   English   中英

C#MySQL語法錯誤

[英]C# MySQL syntax error

我明白了:

您的SQL語法有錯誤; 查看與您的MySQL服務器版本對應的手冊,以便在''spectrum'附近使用正確的語法WHERE specId = 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 specId=@index";
  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;
}

我認為問題是光譜周圍的引用。 我該如何刪除它們?

您不能用參數替換表名。 可悲的是,這只是不受支持。 只有WHERE子句中的參數值才能以這種方式替換。

您必須自己進行替換,而不是依賴於MySqlCommand對象。 這樣的事情應該有效:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM