我在表格中创建了图片列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
}
}
}
搜索了许多论坛并且厌倦了他们在每个帖子中提出的建议,但我无法解决..
答案 0 :(得分:1)
您无法正确将图像保存到数据库中。
行alias subl="/mnt/c/Program\ Files/Sublime\ Text\ 2/sublime_text.exe"
将导致
subl .
因为您正在将字节数组转换为字符串,这将仅导致类型名称。
您必须将字节数组转换为HEX字符串,这应该被SQL引擎接受。
答案 1 :(得分:0)
byte[] ImageData;
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
br = new BinaryReader(fs);
ImageData = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
using (con = new MySqlConnection(DBConStr))
{
con.Open();
using (cmd = new MySqlCommand(qry, con))
{
cmd.Parameters.Add("@pimage", MySqlDbType.Blob);
cmd.Parameters["@pimage"].Value = ImageData;
cmd.ExecuteNonQuery();
}
}
这解决了我的问题并且我也能够回复..感谢Honz指出将其转换为刺痛的实际问题:)