2

我正在尝试向我的 vb winform 程序添加一个功能,用户可以将文件(.doc、.docx、.jpg、.pdf)附加到包含其他文本数据的 mdb 文件中。二进制文件和文件名存储在数据库中。带路径的文件名作为变量“fpath”传递。下面是我到目前为止所拥有的(它现在正在工作,这意味着它保存了文件名和二进制数据)。现在,用户如何打开保存的文件?而且,如果它是 .doc 或 PDF 等文件,如何让默认关联程序打开它?. 有人可以帮我做剩下的吗?

这是存储 OLE 对象的代码:

 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
4

1 回答 1

1

经过两周的搜索和尝试一堆方法后,我得到了它的工作。上面的代码已更正,可用于将文件上传到 .mdb 文件。下面的代码将检索它。是的,我知道将文件保存到 mdb 不是最好的,但只有几个 docs 或 pdf,我需要将它们全部放在一个文件中以便用户之间轻松共享。

  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
于 2013-10-05T05:17:27.567 回答