我正在尝试向我的 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