如何在MySQL中将图像存储为blob

时间:2019-06-30 14:09:46

标签: c#

我已经写了一个代码将图像作为Blob存储在MySql数据库中,但是运行代码时出现错误。我无法解决它。

enter image description here

我写的代码如下:

conn.Open();
MySqlCommand cmd = new MySqlCommand();
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms,pictureBox1.Image.RawFormat);
byte[] ImageData = ms.ToArray();

query = "insert into Item([Category_id],[name],[image],[cost],[price]) VALUES([@Category_id],[@name],[@image],[@cost],[@price])";
cmd.CommandText = query;
cmd.Connection = conn;
cmd.Parameters.Add("@Category_id", MySqlDbType.Int64, 11);
cmd.Parameters.Add("@name", MySqlDbType.VarChar, 20);
cmd.Parameters.Add("@image", MySqlDbType.LongBlob);
cmd.Parameters.Add("@cost", MySqlDbType.Int64, 5);
cmd.Parameters.Add("@price", MySqlDbType.Int64, 5);
cmd.Parameters["@Category_id"].Value = cat_id;
cmd.Parameters["@name"].Value = textBox1.Text;
cmd.Parameters["@image"].Value = ImageData;
cmd.Parameters["@cost"].Value = int.Parse(textBox2.Text);
cmd.Parameters["@price"].Value = int.Parse(textBox3.Text);

int RowsAffected = cmd.ExecuteNonQuery();
if (RowsAffected > 0)
    MessageBox.Show("Data inserted in table");

0 个答案:

没有答案