3

私は得る:

SQL 構文にエラーがあります。MySQL サーバーのバージョンに対応するマニュアルで、1 行目の ''spectra' WHERE specId=42' 付近で使用する正しい構文を確認してください。

このコードの実行中:

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;
}

問題は、スペクトルの周りの引用符だと思います。どうすれば削除できますか?

4

1 に答える 1

4

テーブル名をパラメータに置き換えることはできません。残念ながら、それはサポートされていません。この方法で置き換えることができるのは、WHERE句のパラメーター値のみです。

オブジェクトに依存するのではなく、自分で置換を行う必要がありMySqlCommandます。このようなものが機能するはずです:

string tableName = "spectra";
command.CommandText = 
    String.Format( "SELECT imageObj FROM {0} WHERE specId=@index", tableName );
command.Parameters.AddWithValue("@index", index);
于 2012-06-20T00:32:46.317 に答える