Cory
Cory

Reputation: 1283

Save/open OLE object from .mdb in vb winform

I am trying to add a feature to my vb winform program where a user can attach files (.doc, .docx, .jpg, .pdf) to a mdb file that holds other text data. The binary file and the file name are stored in the DB. The file name with path is passed as variable 'fpath'. below is what I have thus far (It is now working, meaning it saves the file name and binary data). Now, how can the user open the saved file? And, if it is a .doc or PDF etc, how do I make the default associated program open it? . Can someone help me with the rest?

Here is the code to store the OLE object:

 Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)        Handles btnClear.Click
    'data connection
    Dim cn As New OleDb.OleDbConnection
    cn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" & _
                           Application.StartupPath & "\data.mdb"
    cn.Open()

    'file name without path
    Dim flName As String = filename.Text 

    'open file from the disk (file path is the path to the file to be opened)
    Using fileStream As FileStream = File.OpenRead(fpath)
        'create new MemoryStream object
        Dim memStream As New MemoryStream()
        memStream.SetLength(fileStream.Length)
        'read file to MemoryStream
        fileStream.Read(memStream.GetBuffer(), 0, CInt(Fix(fileStream.Length)))
        Dim strImage As String = "?"

        Dim arr As Byte()
        arr = memStream.GetBuffer
        Dim cmd As New OleDb.OleDbCommand
        cmd.Connection = cn
        cmd.CommandText = "INSERT INTO tblstudent(name, photo) VALUES( ?, ?)"
        cmd.Parameters.Add("@name", OleDbType.Char).Value = flName
        cmd.Parameters.Add("@photo", OleDb.OleDbType.Binary).Value = arr

        cmd.ExecuteNonQuery()

        MsgBox("Data save successfully!")
        cn.Close()
    End Using
End Sub

Upvotes: 2

Views: 3999

Answers (1)

Cory
Cory

Reputation: 1283

After two weeks of searching and trying a bunch of methods, I got it to work. The code above is corrected and working for uploading a file to a .mdb file. The code below will retrieve it. Yes, I know saving files to a mdb is not the best, but there will only be a couple docs or pdf, and I need it all in one file for easy sharing between users.

  Private Builder As New OleDbConnectionStringBuilder With _
  { _
      .DataSource = IO.Path.Combine(Application.StartupPath & "\data.mdb"), _
      .Provider = "Microsoft.Jet.OleDb.4.0" _
  }

Private Sub btnGetfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetfile.Click
    Dim selfile As String = fileDgv.CurrentCell.Value.ToString

    Dim cn As New OleDbConnection With {.ConnectionString = Builder.ConnectionString}
    Dim cmd As New OleDbCommand With _
        { _
            .Connection = cn, _
            .CommandText = "SELECT photo FROM tblstudent WHERE name='" & selfile & "'" _
        }
    Dim NoDataList As New List(Of String)


    Dim dr As OleDbDataReader = Nothing
    Dim FileStream As System.IO.FileStream
    Dim Reader As OleDbDataReader
    Dim Data() As Byte = Nothing
    Dim Writer As System.IO.BinaryWriter = Nothing
    Dim bufferSize As Integer = 1000
    Dim buffer(bufferSize - 1) As Byte
    Dim startIndex As Long = 0
    Dim numberOfBytes As Long = 0
    cn.Open()
    Reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
    Reader.Read()
    FileStream = New System.IO.FileStream(
        IO.Path.Combine("C:\temp3", "temp", selfile),
        System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write)
    Writer = New System.IO.BinaryWriter(FileStream)
    Do
        numberOfBytes = Reader.GetBytes(0, startIndex, buffer, 0, bufferSize)
        If numberOfBytes = 0 Then
            Exit Do
        End If
        Writer.Write(buffer, 0, CInt(Fix(numberOfBytes)))
        startIndex += numberOfBytes
    Loop While True
    Writer.Flush()
    If Writer IsNot Nothing Then
        Writer.Close()
    End If
    If FileStream IsNot Nothing Then
        FileStream.Close()
    End If
    If Reader IsNot Nothing Then
        Reader.Close()
    End If
    cn.Close()
    MessageBox.Show("Done")

End Sub

Upvotes: 1

Related Questions