在mysql中更新一个mediumblob? [英] update a mediumblob in mysql?

查看:111
本文介绍了在mysql中更新一个mediumblob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想用图像数据更新中期。

我正在使用MySQL,VS 2013

该表定义为

Pat_Picture_ID = int

Patient_ID = int

BildDaten =中等blob



使用的函数:

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



我试过几种方式:

1)


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



结果:超时已过期。操作完成前经过的超时时间或服务器没有响应。



2)


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





结果:超时已过期。操作完成前经过的超时时间或服务器未响应。

在这些操作之后检查BildDaten字段的内容是13字节(系统数组)



只是为了检查插入我试过



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"]));





我尝试了至少10种不同的建议,我发现谷歌搜索。一切都没有结果。

知道任何人的解决方案吗?

提前谢谢MiKr41



I tried at least 10 different suggestions I found googling. All without result.
Knows anyone a solution?
Thanks in advance MiKr41

推荐答案

我也会拉掉我留下的任何头发(太晚了) - 我认为这个..



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





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



我记得在网上看到一些关于MySQL服务器端blob需要的特殊参数的讨论,最后我不需要它们



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


这篇关于在mysql中更新一个mediumblob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆