none
Read ole object from access database RRS feed

  • Question

  • Hello,

    There is an OLE object column that contains images in my access database. I'm trying to create a program that reads the images from that ole column and store the images to a file. Currently, I have a program that creates an image file but the image is blank. Can someone take a look at my code?

    Option Explicit On
    Imports System.IO
    
    Public Class Form1
      Private cn As ADODB.Connection
      Private rs As ADODB.Recordset
      Dim RecCount As Integer
      Dim image As String = "C:\Image\"
      Dim format As String = ".Jpeg"
      'Dim c As New cDibSection
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        cn = New ADODB.Connection
        cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                   "Data Source=C:\Database3.accdb"
        cn.Open()
        rs = New ADODB.Recordset
        rs.Open("Select * FROM Table1", cn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockBatchOptimistic, 1)
    
        Do While Not rs.EOF
          'MsgBox(rs.Fields("firstname").Value)
          'MsgBox(rs.Fields("lastname").Value)
          'Dim name As String = rs.Fields("Field2").Value
          'MsgBox(name)
          'Dim strRes As String = String.Concat(image, name, format)
    
          'Dim ms As New IO.MemoryStream(CType(rs.Fields("Field2").Value, Byte()))
          Dim saveFile As FileStream = File.Create("C:\Image\image1.Jpg")
          Dim data As Byte() = DirectCast(rs.Fields("Field1").Value, Byte())
          saveFile.Write(data, 0, data.Length)
          saveFile.Close()
    
          'PictureBox1.Image.Save("C)
          rs.MoveNext()
        Loop
    
        cn.Close()
      End Sub
    
      Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
    
      End Sub
    End Class
    

    Wednesday, March 30, 2011 11:21 PM

All replies

  • I think the key question is whether the images where saved to the OLE Object field with OLE headers. Can you confirm this?
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Thursday, March 31, 2011 7:38 PM
  • I don't understand what that means.  The way I saved my images into the access database was by dragging and dropping them into an OLE column.
    Thursday, March 31, 2011 8:23 PM
  • I would say they probably contain the OLE header, which contains information about the type of object and the application that was used to save it. You can verify this by double clicking on the column for a row in Microsoft Access and see if it displays in an application (such as MS Paint).

    The bottom line is that this header needs to be stripped from the data before it can be saved as a file or displayed. Are all the images of the same type? 


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Friday, April 1, 2011 3:54 PM
  • When I double clicked on the value, the image was displayed in Window Photo Viewer.  And yes, all of the images are jpeg.
    Saturday, April 2, 2011 3:12 PM
  • I will try to dig up some code to do this.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Tuesday, April 5, 2011 12:18 PM
  • I will try to dig up some code to do this.
    Paul ~~~~ Microsoft MVP (Visual Basic)

    Was puzzled for a long time, respectfully waiting for your results.
    BMP files have the results, in the msdn. Which have to define a structure.

    HOWTO: Retrieving Bitmap from Access and Displaying In Web Page

          Type OBJECTHEADER

            Signature As Integer

            HeaderSize As Integer

            ObjectType As Long

            NameLen As Integer

            ClassLen As Integer

            NameOffset As Integer

            ClassOffset As Integer

            ObjectSize As PT

            OleInfo As String * 256

          End Type

    Monday, February 6, 2012 8:16 AM
  • respectfully waiting for your results.
    Monday, February 6, 2012 8:25 AM