我已在表中将图像列创建为 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
}
}
}
搜索了很多论坛,厌倦了他们在每个帖子中建议的所有内容,但我无法解决..
最佳答案
您没有正确地将图像保存到数据库中。
行 string qry = "Update admin_info set ``img``='"+ ImageData + "' where id='AD001";
将导致
qry = "Update admin_info set ``img``='System.Byte[]' where id='AD001
因为您要将字节数组转换为字符串,这只会产生类型名称。
您必须将字节数组转换为 SQL 引擎应该接受的十六进制字符串。
关于c# - 从 mysql 检索保存的图像时出现参数无效错误 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40135030/