【发布时间】:2016-10-20 00:09:07
【问题描述】:
我在表中创建了图像列mediumblob
为了保存图像,我使用了以下代码
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);
它是成功的,但是在使用下面的代码将它检索到图片框时,我的参数无效
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
}
}
}
搜索了很多论坛,厌倦了他们在每个帖子中提出的所有建议,但我无法解决..
【问题讨论】:
-
我认为您会发现保存的数据已被您的存储方法损坏 - 请参阅此处了解保存图像的正确方法。 stackoverflow.com/questions/13208349/…
-
是的,你是 ryt.. 谢谢
标签: c# mysql .net winforms visual-studio