2016-10-19 16 views
0

から保存した画像をretrivingながら、私は次のコードパラメータではありません有効なエラーのC#のmysql

byte[] ImageData; 
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); 
ImageData = new byte[Convert.ToInt32(fs.Length)]; 
fs.Read(ImageData, 0, Convert.ToInt32(fs.Length)); 
fs.Close(); 

string qry = "Update admin_info set `img`='"+ ImageData + "' where id='AD001"; 

using (con = new MySqlConnection(DBConStr)) 
{ 
    con.Open(); 
    using (cmd = new MySqlCommand(qry, con)) 
    { 
     cmd.ExecuteNonQuery(); 
    } 
} 
    MessageBox.Show(" Profile Picture Updated Successfully!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information); 

を使用し、それがsucessfullだったが、mは、パラメータが無効になっ以下のコードを使用して画像ボックスに取り込み中

using (MySqlConnection conn = new MySqlConnection("Server = localhost; Port = 3307; database = attendance; uid = root; pwd = MJN45720!")) 
{ 
    conn.Open(); 
    string myQuery = "SELECT img FROM admin_info where id='AD001'"; 

    using (MySqlCommand cmd = new MySqlCommand(myQuery, conn)) 
    { 
     using (var reader = cmd.ExecuteReader()) 
     { 
       while (reader.Read()) 
       { 
        byte[] x = (byte[])reader["img"]; 
        MemoryStream ms = new MemoryStream(x); 
        pictureBox1.Image = new Bitmap(ms); //Parameter invalid in this line 
       } 
     } 
    }   

多くのフォーラムを検索し、すべての投稿で示唆しているすべてが疲れていますが、できません解決..

+0

を刺すためにそれを変換するための実際の問題を指摘するためには、あなたによって破壊されましたそれを保存する方法 - 画像を保存する正しい方法についてはこちらを参照してください。 http://stackoverflow.com/questions/13208349/how-to-insert-blob-datatype – PaulF

+0

はい、私はryt ..ありがとう – Manjunath

答えて

1

DBに画像を正しく保存しません。 行string qry = "Update admin_info set ``img``='"+ ImageData + "' where id='AD001";は、バイト配列を文字列に変換するため、タイプ名のみになるため、 qry = "Update admin_info set ``img``='System.Byte[]' where id='AD001になります。 バイト配列をHEX文字列に変換する必要があります。このHEX文字列は、SQLエンジンで受け付ける必要があります。私はあなたが、データが保存されて見つけることになると思い

+0

指摘してくれてありがとう..私はSQLパラメータを使用してそれを解決qry – Manjunath

0
 byte[] ImageData; 
     fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); 
     br = new BinaryReader(fs); 
     ImageData = br.ReadBytes((int)fs.Length); 
     br.Close(); 
     fs.Close(); 

     using (con = new MySqlConnection(DBConStr)) 
     { 
      con.Open(); 
      using (cmd = new MySqlCommand(qry, con)) 
      { 
       cmd.Parameters.Add("@pimage", MySqlDbType.Blob); 
       cmd.Parameters["@pimage"].Value = ImageData; 
       cmd.ExecuteNonQuery(); 
      } 
     }  

これは私のprobelm、あまりにもretriveすることができイムを解決..おかげHonz :)

関連する問題