0

I'm trying to insert binary data into Attachment type field in Access from C# using OleDb provider.

Error: System.Data.OleDb.OleDbException: An INSERT INTO query cannot contain a multi-valued field.at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)at System.Data.OleDb.OleDbCommand.ExecuteReader()at SDKEnrollApp.Enroll.Run()

Code of the query:

com.CommandText = "INSERT INTO PersonFinger ([PersID], [FingerImage]) VALUES ('@PID', '@IMG')";
com.Parameters.AddWithValue("@PID", m_form.m_clientID);
com.Parameters.Add("@IMG", OleDbType.Binary,baImage.Length).Value = baImage;
dt = new DataTable();
dt.Load(com.ExecuteReader());

baImage is a byte array. Please, help me.

UPDATE

Now the code is:

com.CommandText = "INSERT INTO PersonFinger ([PersID], [FingerImage]) VALUES (@PID, @IMG)";
com.Parameters.AddWithValue("@PID", m_form.m_clientID);
com.Parameters.Add("@IMG", OleDbType.Binary).Value = baImage;
com.ExecuteNonQuery();

The problem still remains.

I've also tried to change field type to OLE object instead of Attachment. In this case the size of the saved file is always 12.

It is not about image file type. It is about byte array so I don't understand why it is marked as duplicate.

mistique
  • 111
  • 1
  • 1
  • 8
  • 1
    Isn't a `byte[]` a "multi-valued" field? – CodingGorilla Mar 15 '16 at 16:42
  • 2
    Try removing the `'` from the `VALUES`, I'm sure they don't belong there (should be `VALUES (@PID, @IMG)`) – René Vogt Mar 15 '16 at 16:43
  • How is `PersonFinger.FingerImage` defined in Access? If it's an attachment field, that is the cause of the multi-valued field complaint. – HansUp Mar 15 '16 at 16:45
  • @CodingGorilla, I'm sorry, mb I don't understand completely what this term means – mistique Mar 15 '16 at 16:45
  • @RenéVogt already tried. Not working – mistique Mar 15 '16 at 16:46
  • @HansUp yes, it is attachment. And what type it should be? – mistique Mar 15 '16 at 16:47
  • Please include the code used to get baImage. Thanks and regards, – Alexander Bell Mar 15 '16 at 17:10
  • 1
    You've got several issues. The single quotes in your `VALUES()` section shouldn't be there. You should be calling `ExecuteNonQuery()` instead of `ExecuteReader()` to run the insert statement. It's not clear what you expect `ExecuteReader()` to return from an insert statement that you could put into a DataTable.As to the specific error, you might try removing the `baImage.Length` parameter and see if that works. – Mr. T Mar 15 '16 at 18:42
  • @Alex Bell `baImage = new byte[templateLen3]; Array.Copy(template3, baImage, templateLen3);` – mistique Mar 16 '16 at 07:09
  • @Mr. T Thank you very much. Removed the single quotes. ExecuteReader was a stupid mistake from copypaste. Thanks for that comment. Removing of the baImage.Length parameter didn't solve the problem. – mistique Mar 16 '16 at 07:17
  • *"It is not about image file type. It is about byte array so I don't understand why it is marked as duplicate."* - No, it isn't. The question title and the bulk of the question text is about inserting an image into an `Attachment` field. If you have changed tactics and now want to insert the image into a BLOB (`OLE Object`) field then ask a new question. – Gord Thompson Mar 16 '16 at 12:11
  • @Gord Thompson _"baImage is a byte array."_ And I don't want to change tactics. I just want to understand how I can store binary data in Access database. If the problem is in field type - mb someone can provide information about it. – mistique Mar 17 '16 at 14:28
  • @Gord Thompson _"bulk of the question text is about inserting an image"_ baImage is just a name of the byte array. using the word "Image" doesn't mean it is image file type. – mistique Mar 17 '16 at 14:35

0 Answers0