0

I need to insert an image in access database that has a column of type OLE Object I tried to use the following code but it gives me a Syntax Error in insert staetment but I am sure of table name and column name and the imageContent value is not null

    System.Data.OleDb.OleDbConnection MyConnection;
    private void Insert_Customer_Load(object sender, EventArgs e)
    {
        string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Customer.accdb;Persist Security Info=True;Jet OLEDB:Database Password=123";
        MyConnection = new System.Data.OleDb.OleDbConnection(strConn);
    }

    private void InsertBtn_Click(object sender, EventArgs e)
    {
        System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
        string sql = null;

        MyConnection.Open();
        myCommand.Connection = MyConnection;
        byte[] imageContent = File.ReadAllBytes(imagePathTxt.Text);
        sql = "INSERT INTO [Customer_Data] (Image) VALUES (@photo)";
        myCommand.CommandText = sql;
        OleDbParameter ph = new OleDbParameter("@photo", OleDbType.VarBinary);
        ph.Value = imageContent;
        ph.Size = imageContent.Length;
        myCommand.Parameters.Add(ph);
        myCommand.ExecuteNonQuery();
        MyConnection.Close();
    }

Also I tried to use ? instead of @photo but also the same exception raised.

Thanks in advance

2 Answers 2

2

I think Image is a reserved word, so maybe it's just that you should use [Image] or, better, rename the column. (BTW, are you sure you want to store images in your database?)

Sign up to request clarification or add additional context in comments.

Comments

1

rename the column you should use [Image]

1 Comment

Please be more specific. Which line of code are you talking about? Where is the column that should be renamed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.