Click here to Skip to main content
15,906,081 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
I am working on a windows application where i need to browse pdf files and store them directly into the database instead of storing file path. can anyone please tell me how can i store the file directly into the database and then also retrieve it back and show it in pdf viewer or in some similar control when needed ??

thanks in advance......


Regards,
Aradhana
Posted

VB.NET
Imports System.Data.SqlClient

'IF OBJECT_ID('<strong class="highlight">PDF</strong>', 'U') IS NOT NULL DROP TABLE <strong class="highlight">PDF</strong>
'CREATE TABLE <strong class="highlight">PDF</strong>
'(
'  RecordId int identity(1000, 1) PRIMARY KEY,
'  <strong class="highlight">PDF</strong> 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("<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
 
Share this answer
 
 
Share this answer
 
Comments
SoMad 23-Jan-13 0:17am    
Don't wake up questions that are over a year old and have an accepted answer. I am not downvoting you, but please don't do it again.

Soren Madsen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900