Hi,
I would like to update a mediumblob with image data.
I am using MySQL, VS 2013
The table is defined with
Pat_Picture_ID = int
Patient_ID = int
BildDaten = medium blob
Used functions:
public Image ByteArrayToImage(byte[] inp)
{
MemoryStream ms = new MemoryStream(inp);
return Image.FromStream(ms);
}
public byte[] ImageToByteArray(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fs);
return reader.ReadBytes((int)fs.Length);
}
public byte[] ImageToByteArray(Image img)
{
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
I tried in several ways:
1)
DBPic = new MySqlDataAdapter("select * from pat_picture_old where Pat_Picture_ID = 9972", DBC);
sPic = new DataSet();
DBPic.Fill(sPic);
sPic.Tables[0].Rows[0]["BildDaten"] = ImageToByteArray(@"C:\Users\Michael\Pictures\Paris 2010\3\12-14037.jpg");
MySqlCommandBuilder cmb = new MySqlCommandBuilder(DBPic);
DBPic.UpdateCommand = cmb.GetUpdateCommand();
try
{
if (DBPic.Update(sPic) > 0)
richTextBoxExtended1.RichTextBox.AddImage(ByteArrayToImage((byte[])sPic.Tables[0].Rows[0]["BildDaten"]));
}
catch (MySqlException mEX)
{
MessageBox.Show(mEX.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Result: "timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding".
2)
MySqlCommand cmd = new MySqlCommand("update pat_picture_old set BildDaten = ?bData where Pat_Picture_ID = 9972", DBC);
cmd.Parameters.Add("?bData", MySqlDbType.MediumBlob).Value = ImageToByteArray(@"C:\Users\Michael\Pictures\Paris 2010\3\12-14037.jpg");
try
{
if (cmd.ExecuteNonQuery() > 0)
richTextBoxExtended1.RichTextBox.AddImage(ByteArrayToImage((byte[])sPic.Tables[0].Rows[0]["BildDaten"]));
}
catch (MySqlException mEX)
{
MessageBox.Show(mEX.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Result: "timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding".
Inspecting the BildDaten field after these actions the content is 13 Bytes (System Array)
Just to check Inserting I tried
rPic = sPic.Tables[0].NewRow();
rPic["Patient_ID"] = 233;
rPic["BildDaten"] = ImageToByteArray(@"C:\Users\Michael\Pictures\Paris 2010\3\12-14037.jpg");
sPic.Tables[0].Rows.Add(rPic);
DBPic.InsertCommand = cmb.GetInsertCommand();
try
{
if (DBPic.Update(sPic) > 0)
richTextBoxExtended1.RichTextBox.AddImage(ByteArrayToImage((byte[])sPic.Tables[0].Rows[1]["BildDaten"]));
}
catch (MySqlException mEX)
{
MessageBox.Show(mEX.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Result: "timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding".
Reading data worked without any problem
DBC = new MySqlConnection(strConn);
DBC.Open();
MySqlDataAdapter DBPic = new MySqlDataAdapter("select * from pat_picture_old where Pat_Picture_ID = 9971", DBC);
DataSet sPic = new DataSet();
DBPic.Fill(sPic);
DataRow rPic = sPic.Tables[0].Rows[0];
richTextBoxExtended1.RichTextBox.AddImage(ByteArrayToImage((byte[])rPic["BildDaten"]));
I tried at least 10 different suggestions I found googling. All without result.
Knows anyone a solution?
Thanks in advance MiKr41