I am trying to insert an image in my access database from C# winform. I am using the following code:
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb");
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = "INSERT INTO Table1 (Product, Manufacturer, Description, Price, Image) VALUES ('Column1', 'Column2', 'Column3', 'Column4', @img)";
byte[] yourPhoto = imageToByteArray(pictureBox1.Image);
cmd.Parameters.AddWithValue("@img", yourPhoto);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public byte[] imageToByteArray(System.Drawing.Image iImage)
{
MemoryStream mMemoryStream = new MemoryStream();
iImage.Save(mMemoryStream, System.Drawing.Imaging.ImageFormat.Png);
return mMemoryStream.ToArray();
}
When I run the code it show me an error: Syntax error in INSERT INTO statement.
What is wrong here in my code? I can successfully insert text to the fields of database by using the same query.
Image
is a reserved word, so I assume you should put this word in quotes or square brackets in the SQL query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With