Save PDF file in a database

I want to save a pdf file in the database. can someone point me to a sample of vb or asp code that will do this.

i know it is not a good idea to save it in the database but the business requirements need this to be in the databsase.

Thanks,
AJ.
LVL 1
avisjkAsked:
Who is Participating?
 
Anthony PerkinsConnect With a Mentor Commented:
Sub AddImage(rs As ADODB.Recordset, ByVal FileName As String)
Dim stm As ADODB.Stream

Set stm = New ADODB.Stream
With stm
  .Type = adTypeBinary
  .Open
  .LoadFromFile FileName
 
  'Insert the binary object into the table.
  rs.AddNew
  rs.Fields("PDFColumnName").Value = .Read
  rs.Update
  .Close
End With
Set stm = Nothing

End Sub

Sub ShowPDF(rs As ADODB.Recordset, ByVal FileName As String)
' Const SW_SHOW = 5
Dim stm As ADODB.Stream

FileName = CStr(Timer * 100) & ".pdf"
If Not IsNull(rs.Fields("PDFColumnName").Value) Then
    Set stm = New ADODB.Stream
    With stm
       .Type = adTypeBinary
       .Open
       .Write rs.Fields("PDFColumnName").Value
       .SaveToFile TempFileName, adSaveCreateOverWrite
       .Close
    End With
    Set stm = Nothing
    ' ShellExecute Me.hWnd, vbNullString, TempFileName, vbNullString, vbNullString, SW_SHOW
End If

End Sub

Anthony
0
 
Anthony PerkinsCommented:
Let's try that again:

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
                                                                                                        ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Sub AddImage(rs As ADODB.Recordset, ByVal FileName As String)
Dim stm As ADODB.Stream

Set stm = New ADODB.Stream
With stm
  .Type = adTypeBinary
  .Open
  .LoadFromFile FileName
 
  'Insert the binary object into the table.
  rs.AddNew
  rs.Fields("PDFColumnName").Value = .Read
  rs.Update
  .Close
End With
Set stm = Nothing

End Sub

Sub ShowPDF(rs As ADODB.Recordset, ByVal FileName As String)
Const SW_SHOW = 5
Dim stm As ADODB.Stream

If Not IsNull(rs.Fields("PDFColumnName").Value) Then
    Set stm = New ADODB.Stream
    With stm
       .Type = adTypeBinary
       .Open
       .Write rs.Fields("PDFColumnName").Value
       .SaveToFile FileName, adSaveCreateOverWrite
       .Close
    End With
    Set stm = Nothing
    ShellExecute Me.hWnd, vbNullString, TempFileName, vbNullString, vbNullString, SW_SHOW
End If

End Sub

Anthony
0
 
avisjkAuthor Commented:
I need to do this from asp.net, so i will try to convert this code to asp.net code and check if it works. do you guys haves an asp.net (vb) code sample.

Thanks,
AJ
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
pdrgCommented:
My first question would have to be why do you want to save a pdf to a database?  Could you save yourself a lot of grief and database bloat by saving to a file and saving a link to the file?

Remember, once you've got the file in there, you need to get it out again, and that's just as fun as getting it in there.

Good reasons to do so would be few and far between imho, but configuration management is a possible.  However securing down your server intelligently should mean you're not exposed.

hth

Paddy
0
 
Computer101Commented:
A request for deletion has been made.  If no response or you feel this is in error, comment.  If no objection, I will delete in three days.

Computer101
E-E Admin
0
 
Anthony PerkinsCommented:
The question should have been phrased better.  Rather than "sample of vb or asp code", it should have been clearly stated that it was for .NET.

Having said that I do not honestly care if the question is deleted.

Anthony
0
All Courses

From novice to tech pro — start learning today.