Imports System.Data.SqlClient
Public Class frmPDF
Private Sub ButtonUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonUpload.Click
Using conn As New SqlConnection("<strong class="highlight">Data</strong> Source=apex2006sql;Initial Catalog=Scott;Integrated Security=True;")
conn.Open()
Using cmd As New SqlCommand("Insert Into <strong class="highlight">PDF</strong> (<strong class="highlight">PDF</strong>) Values (@PDF)", conn)
cmd.Parameters.Add(New SqlParameter("@PDF", SqlDbType.VarBinary)).Value = System.IO.File.ReadAllBytes("C:\file.pdf")
cmd.ExecuteNonQuery()
End Using
conn.Close()
End Using
End Sub
Private Sub ButtonDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDownload.Click
Dim sFilePath As String
Dim buffer As Byte()
Using conn As New SqlConnection("<strong class="highlight">Data</strong> Source=apex2006sql;Initial Catalog=Scott;Integrated Security=True;")
conn.Open()
Using cmd As New SqlCommand("Select Top 1 <strong class="highlight">PDF</strong> <strong class="highlight">From</strong> <strong class="highlight">PDF</strong>", conn)
buffer = cmd.ExecuteScalar()
End Using
conn.Close()
End Using
sFilePath = System.IO.Path.GetTempFileName()
System.IO.File.Move(sFilePath, System.IO.Path.ChangeExtension(sFilePath, ".pdf"))
sFilePath = System.IO.Path.ChangeExtension(sFilePath, ".pdf")
System.IO.File.WriteAllBytes(sFilePath, buffer)
Dim act As Action(Of String) = New Action(Of String)(AddressOf OpenPDFFile)
act.BeginInvoke(sFilePath, Nothing, Nothing)
End Sub
Private Shared Sub OpenPDFFile(ByVal sFilePath)
Using p As New System.Diagnostics.Process
p.StartInfo = New System.Diagnostics.ProcessStartInfo(sFilePath)
p.Start()
p.WaitForExit()
Try
System.IO.File.Delete(sFilePath)
Catch
End Try
End Using
End Sub
End Class