See the answer here:
Save and view pdf file from SQL server database in c# WinForms[
^]
The code in the example seems to have some garbage in it though, like:
<strong class="highlight">
It should look like this I think:
Imports System.Data.SqlClient
'IF OBJECT_ID('
PDF', 'U') IS NOT NULL DROP TABLE
PDF
'CREATE TABLE
PDF
'(
' RecordId int identity(1000, 1) PRIMARY KEY,
'
PDF varbinary(max)
')
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("
Data Source=apex2006sql;Initial Catalog=Scott;Integrated Security=True;")
conn.Open()
Using cmd As New SqlCommand("Insert Into
PDF (
PDF) 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("
Data Source=apex2006sql;Initial Catalog=Scott;Integrated Security=True;")
conn.Open()
Using cmd As New SqlCommand("Select Top 1
PDF From PDF", 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