tutte le soluzioni presentate non funziona .. dont concentrarsi solo sulla parte recupero. luk all'inserimento dell'immagine. ho fatto lo stesso errore Tiro un'immagine dal disco rigido e l'ho salvata nel database. Il problema sta nel comando insert. luk al mio codice di errore ..:
public bool convertImage()
{
try
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
photo = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo, 0, photo.Length);
return true;
}
catch
{
MessageBox.Show("image can not be converted");
return false;
}
}
public void insertImage()
{
// SqlConnection con = new SqlConnection();
try
{
cs.Close();
cs.Open();
da.UpdateCommand = new SqlCommand("UPDATE All_students SET disco = " +photo+" WHERE Reg_no = '" + Convert.ToString(textBox1.Text)+ "'", cs);
da.UpdateCommand.ExecuteNonQuery();
cs.Close();
cs.Open();
int i = da.UpdateCommand.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Successfully Inserted...");
}
}
catch
{
MessageBox.Show("Error in Connection");
}
cs.Close();
}
Il codice sopra mostra con successo inserita ... ma actualy sua salvare l'immagine sotto forma di tipo di dati sbagliato .. considerando che il tipo di dati deve BT "immagine" .. così ho migliorato il codice ..
public bool convertImage()
{
try
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
photo = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo, 0, photo.Length);
return true;
}
catch
{
MessageBox.Show("image can not be converted");
return false;
}
}
public void insertImage()
{
// SqlConnection con = new SqlConnection();
try
{
cs.Close();
cs.Open();
//THIS WHERE THE CODE MUST BE CHANGED>>>>>>>>>>>>>>
da.UpdateCommand = new SqlCommand("UPDATE All_students SET disco = @img WHERE Reg_no = '" + Convert.ToString(textBox1.Text)+ "'", cs);
da.UpdateCommand.Parameters.Add("@img", SqlDbType.Image);//CHANGED TO IMAGE DATATYPE...
da.UpdateCommand.Parameters["@img"].Value = photo;
da.UpdateCommand.ExecuteNonQuery();
cs.Close();
cs.Open();
int i = da.UpdateCommand.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Successfully Inserted...");
}
}
catch
{
MessageBox.Show("Error in Connection");
}
cs.Close();
}
100% gurantee che non ci sarà alcun nON PARAMETRO errore valido nel recupero .... RISOLTO !!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!
fonte
2012-05-08 09:39:44
per favore formattare la tua domanda correttamente –
'nessun valore in esso non è maggiore di 255' è errato, il suo doppio negativo, rendendolo positivo. Quindi tu dici che tutti i valori sono superiori a 255. Immagino tu intenda che sono tutti inferiori o uguali a 255, che è una barriera intrinseca di un byte. –