我正在尝试从Windows应用程序向mysql数据库插入图像。在尝试这样做时,我遇到了以下错误
“无法将类型为'System.Byte []'的对象转换为类型为'System.IConvertible'。”
public void LoadImages()
{
MySqlConnection cn = new MySqlConnection(connstring);
cn.Open();
string image = txtLogo.Text;
byte[] ImageData;
FileStream fs = new FileStream(image, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
ImageData = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
MySqlCommand cmd = new MySqlCommand("insert into Fn_Pictures(Images,Email)values(@Images,'"+txtEmailId.Text+"')", cn);
cmd.Parameters.AddWithValue("@Images", MySqlDbType.LongBlob).Value = ImageData;
cmd.ExecuteNonQuery();
cn.Close();
}
请帮助清除此错误。
这应该做:
MySqlCommand cmd = new MySqlCommand("insert into Fn_Pictures(Images,Email)values(?Images,'" + txtEmailIdText + "')", cn);
MySqlParameter parImage = new MySqlParameter();
parImage.ParameterName = "?Images";
parImage.MySqlDbType = MySqlDbType.MediumBlob;
parImage.Size = 3000000;
parImage.Value = ImageData;//here you should put your byte []
cmd.Parameters.Add(parImage);
cmd.ExecuteNonQuery();
这可能是检查映像是否已存储到数据库的一种方法
//write your code to get the record from db to byte[] ImageData;
public Image byteArrayToImage(byte[] byteBLOBData )
{
MemoryStream ms = new MemoryStream(byteBLOBData );
Image returnImage = Image.FromStream(ms);
return returnImage;
}
像这样打电话
PictureBox picture = new PictureBox();
picture.Image = byteArrayToImage(ImageData);
Controls.Add(picture);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句