使用VB.NET将PDF文件插入MYSQL服务器 [英] Inserting PDF files to MYSQL server using VB.NET
问题描述
我对此很新,但我要做的是通过VB.NET将PDF文件保存到MYSQL服务器,到目前为止,我已设法使用以下代码保存图像文件(下面的代码)显示如何显示图像文件):
I'm fairly new to this but what I'm trying to do is save PDF files to a MYSQL server through VB.NET, so far I have managed to save image files with the following code (code below shows how to display image file):
Try
Dim OFD As FileDialog = New OpenFileDialog()
OFD.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif"
If OFD.ShowDialog() = DialogResult.OK Then
imgpath = OFD.FileName
PictureBox1.ImageLocation = imgpath
End If
OFD = Nothing
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
下面的代码显示了将图像文件保存到MYSQL数据库的方法。
And the code below shows the method of saving the image file to a the MYSQL database.
Try
Dim mstream As New System.IO.MemoryStream()
PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
arrImage = mstream.GetBuffer()
Dim FileSize As UInt32
FileSize = mstream.Length
mstream.Close()
conn.ConnectionString = Myconnection
conn.Open()
sql = "insert into studentsubmissions(content, submissionid, studentnumber, time, date, deadline, title, work, modulename) VALUES (@filestuff, @subid, @stunumber, @subtime, @subdate, @workdeadline, @stutitle, @stuwork, @workmodulename)"
cmd.Connection = conn
cmd.CommandText = sql
cmd.Parameters.AddWithValue("@filestuff", arrImage)
cmd.Parameters.AddWithValue("@subid", idbox.Text)
cmd.Parameters.AddWithValue("@stunumber", usernameconstant.Text)
cmd.Parameters.AddWithValue("@subtime", todaystime.Text)
cmd.Parameters.AddWithValue("@subdate", todaysdate.Text)
cmd.Parameters.AddWithValue("@workdeadline", deadlinesubmission.Text)
cmd.Parameters.AddWithValue("@stutitle", titlesubmission.Text)
cmd.Parameters.AddWithValue("@stuwork", workload.Text)
cmd.Parameters.AddWithValue("@workmodulename", modulename.Text)
cmd.ExecuteNonQuery()
MessageBox.Show("Created")
cmd.Parameters.Clear()
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
下面是我用于在Adobe PDF阅读器中显示PDF文件的方法
Finally below is the method I am using to display a PDF file in an Adobe PDF Reader
Dim opf As New OpenFileDialog
opf.Filter = "PDF File | *.pdf"
If opf.ShowDialog = DialogResult.OK Then
AxAcroPDF1.src = opf.FileName
text_file.Text = opf.SafeFileName
End If
我没有将图像保存到数据库中而是想保存PDF,我发现的代码只处理将图像保存到MYSQL但我是努力翻译保存图像的代码以支持PDF。我已经尝试过研究可能的解决方案
但我没有遇到太多在线问题。试图翻译"保存"方法的前几行以支持PDF是我正在努力的主要领域。
Instead of saving images into a database I want to save PDFs instead, the code I found only deals with saving images to a MYSQL but I am struggling to translate the code that saves images to support PDFs instead. I have tried researching on possible solutions but I haven't encountered much online. Trying to translate the first few lines of the 'Saving' method to support PDF is the main area I'm struggling in.
推荐答案
只需使用FileStream类将文件读入Byte数组,然后插入数据库表即可。无需将其加载到Acrobat组件中即可。
Just read the file into a Byte array using a FileStream Class and then INSERT into the database table. There is no need to load it into the Acrobat component in order to do this.
https://dev.mysql.com/doc/connector-net/en/connector-net-programming-blob-writing.html
这篇关于使用VB.NET将PDF文件插入MYSQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!