Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-server-2005 C# SQL-Server , +
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 6-Jan-12 0:33am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Comments
SoMad at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

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

  Print Answers RSS
0 Mathew Soji 334
1 Maciej Los 295
2 BillWoodruff 290
3 Afzaal Ahmad Zeeshan 257
4 DamithSL 225
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 23 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100