2 Replies - 5085 Views - Last Post: 30 March 2011 - 09:44 PM Rate Topic: -----

#1 hubelial  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-March 11

Read and store ole object from access database

Posted 30 March 2011 - 05:38 PM

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



Is This A Good Question/Topic? 0
  • +

Replies To: Read and store ole object from access database

#2 Elda  Icon User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 314
  • Joined: 30-December 10

Re: Read and store ole object from access database

Posted 30 March 2011 - 08:42 PM

This maybe could help to save image.


 Dim ms As New MemoryStream()
            picturebox1.Image.Save(ms, picturebox1.Image.RawFormat)
            Dim arrImage() As Byte = ms.GetBuffer
            ms.Close()    'It is good to always close the stream rather than
            ' to leave it for the garbage collector

            Dim strFilename As String = lblFilePath.Text.Substring(lblFilePath.Text.LastIndexOf("\") + 1)


Was This Post Helpful? 0
  • +
  • -

#3 hubelial  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-March 11

Re: Read and store ole object from access database

Posted 30 March 2011 - 09:44 PM

I'm REALLY new to VB.NET so I find it hard to understand what your code is doing >.< especially that last line. Can you please step me through it?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1