Question Read ole object from access database

huBelial

New member
Joined
Mar 30, 2011
Messages
2
Programming Experience
Beginner
I have an access database that has a OLE datatype column. What I'm trying to do is create a program that reads the images from that ole column and store the images to a file. Anyways, my program can create an image file but the image is blank. Can someone take a look at my code?

VB.NET:
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
 
Back
Top