1

I have a column in my table stored in MS Access as a Attachment data type. It stores various files such as docx, pdf's etc.

I am trying to display a file from the table using a documentViewer (obtained from XtremeDocumentStudio .NET).

I also have a combo box on my form with a list of employee ID's. When a particular ID is selected from the combo box, I want the associated attachment with that employee to be displayed in the documentViewer.

I am using this query:

SELECT EmployeeAttachment FROM Employee WHERE EmployeeID = 2

I have been stuck on this problem for a while and am not sure on how to implement it. Any help or ideas on how I would do this would be greatly appreciated.

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
Lloyd
  • 435
  • 3
  • 12
  • 29
  • Could you clarify? What exactly is the problem...can't select the data, can't display it...what? – DonBoitnott Feb 17 '15 at 20:40
  • I can select the particular attachment fine as shown in the query I provided. But it's the displaying of it in the documentViewer I don't know how to do. – Lloyd Feb 17 '15 at 20:45
  • I am not familiar with XtremeDocumentStudio. I assume you have an instance of the viewer control, correct? If so, what does it want from you? A `Byte[]`? An `Image`? Perhaps you could show a snippet related to that thought. – DonBoitnott Feb 17 '15 at 20:48
  • Yes I have an instance of the viewer control. I'm assuming bytes as described in this document: http://www.gnostice.com/nl_article.asp?id=279&t=How_to_save_and_retrieve_PDF_documents_to_and_from_a_database_using_C# That document seems to be a close solution but I can't seem to understand how it would fit in with my application as I have the documentViewer and not the PDFviewer. – Lloyd Feb 17 '15 at 20:58
  • Looks like your control should have a `LoadDocument()` method just like the PDF one does. And perhaps overloads for both a `String` (full path) and `Byte[]` (as shown in the link you provided). Using Gord's answer, you should be able to leverage the former. Otherwise, the example seems spot on. – DonBoitnott Feb 17 '15 at 21:09

1 Answers1

1

In order to get a faithful copy of the file from the Attachment field in the Access database you need to use the .SaveToFile method of an ACE DAO Field2 object. For details, see the related question:

Extracting files from an Attachment field in an Access database

Once you have extracted the file to disk (e.g., to System.IO.Path.GetTempPath) then you can tell the viewer control where to find it.

Community
  • 1
  • 1
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • thank you for your comment. If I tried to store files on a database in phpMyAdmin would I still have to extract the file to disk? or would it be able to read the file directly from the database? – Lloyd Feb 17 '15 at 22:15
  • If you were to store the attachment file data in MySQL as one of the BLOB column types then you could retrieve the file from the database without saving it to disk first. The same holds true if you were to store the attachment file data as raw binary data in the Access equivalent of a BLOB field, which is the (unfortunately-named) `OLE Object` field type. The requirement to save the file to disk first is due to it being in an `Attachment` field, which offers certain advantages to Access applications (details in the related answer [here](http://stackoverflow.com/a/27015286/2144390)). – Gord Thompson Feb 17 '15 at 22:27
  • ah right ok thanks for that. I think I'll continue trying to store files as an attachment field and retrieving the files first and then move on to try and store the files in phpMyAdmin. I'll let you know how I get on. Thanks for your help. Phil – Lloyd Feb 17 '15 at 22:43
  • Hi Gord, I have decided to store my files in a MySQL database rather than in Access. Have you got any guidance on how I could retrieve BLOB files from the database and display it in a documentViewer? – Lloyd Feb 18 '15 at 15:17
  • *"Have you got any guidance on how I could retrieve BLOB files from the [MySQL] database and display it in a documentViewer?"* - Try your best, and if you run into difficulties then [ask a new question](http://stackoverflow.com/questions/ask) according to the guidelines for [ask] and we will do our best to help you. – Gord Thompson Feb 19 '15 at 00:14
  • Hi Gord. I have been trying my best to try and display BLOB files to the documentViewer I have on the form but not had any luck. I've therefore posted a new question to seek help for solving this issue. The question can be found here: http://stackoverflow.com/questions/28636869/display-blob-file-in-documentviewer-c-sharp – Lloyd Feb 20 '15 at 19:40