Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong here? In my code (Inserting Image into Database - C#)

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.

like image 229
Farid-ur-Rahman Avatar asked Jan 30 '12 08:01

Farid-ur-Rahman


1 Answers

Image is a reserved word, so I assume you should put this word in quotes or square brackets in the SQL query.

like image 134
Mr Lister Avatar answered Nov 14 '22 23:11

Mr Lister