Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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)
C#
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)
C#
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
C#
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
C#
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
Posted

1 solution

I too would have pulled whatever hair I had left (too late) out - I think this ..

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


is not giving you all the bytes you need - the only way I made my insert images was akin to this copied from SO

byte[] rawData = File.ReadAllBytes(@"d:\Untitled.gif");
FileInfo info = new FileInfo(@"d:\Untitled.gif");
int fileSize = Convert.ToInt32(info.Length);
using(MySqlConnection connection = new MySqlConnection("server=192.168.1.104;uid=root;pwd=root;database=cady234;"))
{
    using(MySqlCommand command = new MySqlCommand())
    {
        command.Connection = connection;
        command.CommandText = "INSERT INTO file (file_name, file_size, file) VALUES (?fileName, ?fileSize, ?rawData);";
        MySqlParameter fileNameParameter = new MySqlParameter("?fileName", MySqlDbType.VarChar, 256);
        MySqlParameter fileSizeParameter = new MySqlParameter("?fileSize", MySqlDbType.Int32, 11);
        MySqlParameter fileContentParameter = new MySqlParameter("?rawData", MySqlDbType.Blob, rawData.Length);
        fileNameParameter.Value = "test name";
        fileSizeParameter.Value = fileSize;
        fileContentParameter.Value = rawData;
        command.Parameters.Add(fileNameParameter);
        command.Parameters.Add(fileSizeParameter);
        command.Parameters.Add(fileContentParameter);
        connection.Open();
        command.ExecuteNonQuery();
    }
}


link : http://stackoverflow.com/questions/13208349/how-to-insert-blob-datatype[^]

I remember seeing some discussion on the web about some special parameters that were needed for blobs on the MySQL server side, in the end I didn't need them
 
Share this answer
 
Comments
Member 10284036 14-Nov-14 6:30am    
Thank you for your answer - but still I have this damned timeout error. The Insert command look's like:
<pre?cs>
public byte[] ImageToByteArray(string fileName)
{
byte[] rawData = File.ReadAllBytes(fileName);
FileInfo info = new FileInfo(fileName);
int fileSize = Convert.ToInt32(info.Length);
using (MySqlCommand command = new MySqlCommand())
{
command.Connection = DBC;
command.CommandText = "insert into pat_picture_old (Patient_ID, Datei, BildDaten, DatenLen) values (233, ?fileName, ?rawData, ?fileSize)";
(file_name, file_size, file) VALUES (?fileName, ?fileSize, ?rawData)";
MySqlParameter fileNameParameter = new MySqlParameter("?fileName", MySqlDbType.VarChar, 50);
MySqlParameter fileSizeParameter = new MySqlParameter("?fileSize", MySqlDbType.Int32);
MySqlParameter fileContentParameter = new MySqlParameter("?rawData", MySqlDbType.Blob, rawData.Length);
fileNameParameter.Value = fileName;
fileSizeParameter.Value = fileSize;
fileContentParameter.Value = rawData;
command.Parameters.Add(fileNameParameter);
command.Parameters.Add(fileSizeParameter);
command.Parameters.Add(fileContentParameter);
try
{
command.ExecuteNonQuery();
}
catch (MySqlException mEX)
{
MessageBox.Show(mEX.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
return rawData;
}
</pre>
Member 10284036 14-Nov-14 7:03am    
Additionally I tried an insert without blob data using the same code remaining after deleting ?rawData - this work's perfectly fine.
Garth J Lancaster 14-Nov-14 17:26pm    
have you tried increasing max_allowed_packet (one of the things a google search brings up and says 'you must change if you're using blobs' ?

you could try this

SET GLOBAL max_allowed_packet=1073741824;

from a query or probably the a MySQL console session, which will persist until the server restarts - if it works you can make it permanent
Member 10284036 15-Nov-14 4:18am    
Thank you very much, I hope it will work (I sent it to the stuff of my domain hoping it will be accepted) KR MiKr41

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900