?
Solved

Save PDF file in a database

Posted on 2003-03-18
6
Medium Priority
?
246 Views
Last Modified: 2012-08-14
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.
0
Comment
Question by:avisjk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 150 total points
ID: 8161911
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
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 8161927
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
 
LVL 1

Author Comment

by:avisjk
ID: 8163232
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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
LVL 1

Expert Comment

by:pdrg
ID: 8166763
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
 
LVL 1

Expert Comment

by:Computer101
ID: 8214023
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
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 8214524
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

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question