Question How to write/read image from OLE Object Field in Microsoft Access

lhdvbnet

Member
Joined
Feb 25, 2009
Messages
15
Programming Experience
Beginner
I have a problem now: write/read image from OLE Object Field in Microsoft Access.

I'm just a newbie in vb.net so i have no idea to resolve this problem :(

Can you show me some ideas :confused:

Thank you!

(sorry about may english :p)
 
thank you, but i'm using ado.net (oledb), so i can't understand any sql command
 
OleDb Obtaining BLOB Values from a Database

VB.NET:
Dim pubsConn As New OleDbConnection("connectionstring")
        Dim logoCMD As New OleDbCommand("SELECT pub_id, logo FROM pub_info", pubsConn)

        Dim fs As IO.FileStream                 ' Writes the BLOB to a file (*.bmp).
        Dim bw As IO.BinaryWriter               ' Streams the binary data to the FileStream object.

        Dim bufferSize As Integer = 100      ' The size of the BLOB buffer.
        Dim outbyte(bufferSize - 1) As Byte  ' The BLOB byte() buffer to be filled by GetBytes.
        Dim retval As Long                   ' The bytes returned from GetBytes.
        Dim startIndex As Long = 0           ' The starting position in the BLOB output.

        Dim pub_id As String = ""            ' The publisher id to use in the file name.

        ' Open the connection and read data into the DataReader.
        pubsConn.Open()
        Dim myReader As OleDbDataReader = logoCMD.ExecuteReader(Data.CommandBehavior.SequentialAccess)

        Do While myReader.Read()
            ' Get the publisher id, which must occur before getting the logo.
            pub_id = myReader.GetString(0)

            ' Create a file to hold the output.
            fs = New IO.FileStream("logo" & pub_id & ".bmp", IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
            bw = New IO.BinaryWriter(fs)

            ' Reset the starting byte for a new BLOB.
            startIndex = 0

            ' Read bytes into outbyte() and retain the number of bytes returned.
            retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)

            ' Continue reading and writing while there are bytes beyond the size of the buffer.
            Do While retval = bufferSize
                bw.Write(outbyte)
                bw.Flush()

                ' Reposition the start index to the end of the last buffer and fill the buffer.
                startIndex += bufferSize
                retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
            Loop

            ' Write the remaining buffer.
            bw.Write(outbyte, 0, retval - 1)
            bw.Flush()

            ' Close the output file.
            bw.Close()
            fs.Close()
        Loop

        ' Close the reader and the connection.
        myReader.Close()
        pubsConn.Close()
 
I stored jpeg images in an ole field. I followed your code and it wrote the jpeg images but the images are blank. Can someone tell me why??
 
Back
Top