mwright
mwright

Reputation: 4165

Storing and retrieving dynamically created pdf in sql

I have been playing around with creation of pdf documents for a project that I'm working on. I would like to store the generated pdf document in a SQL database and then later be able to retrieve this pdf as well.

What are some suggestions for doing this? Can the document be stored in the database without physically creating the document on the server?

Upvotes: 1

Views: 1979

Answers (6)

Jose Quispe
Jose Quispe

Reputation: 1

You have 2 ways to do that:

  1. Store in FileServer and store the Filename in the database.
  2. Encode file and store in database.

I recomend that you use the second..
why I choose that answer?? for security.

One of a lot of reasons: A little Example:

  • If you do the Firt(store the file in the fileserver...) you are crating a folder on your database.. so you server will be vulnerable for attacks or for virus..

  • If you do the second. the file will be encode and store in database and you dont need to be worried about attacks or machine infections..

I think that this are 1 simple reason about why never use the first WAY!!!!!

Upvotes: 0

Anthony Faull
Anthony Faull

Reputation: 17957

You can use a table like this to store files in SQL Server.

CREATE TABLE [Documents]
(
    [FileName] nvarchar(1000),
    [FileContent] varbinary(max)
)

Upvotes: 0

John Saunders
John Saunders

Reputation: 161783

Keep in mind that SQL Server 2008 now has the FILESTREAM data type. You can write the data to the file system, yet still store it in a column.

Upvotes: 1

Aaron
Aaron

Reputation: 7541

This is again going to bring up the debate for/against storing things on the file system or within sql server itself.

It really depends on your needs, the size that you're expected, etc. Here are some references, each with more references.

Storing a file in a database as opposed to the file system?

store image in database or in a system file?

Upvotes: 1

Gabe
Gabe

Reputation: 50493

Save the PDF as a byte[] then you can use itextsharp to created the PDF when ready for viewing.

Upvotes: 0

kemiller2002
kemiller2002

Reputation: 115488

What are some suggestions for doing this? Can the document be stored in the database without physically creating the document on the server?

Sure just create the pdf as a byte stream (byte[]) and store it in the database. Depending on what you use to create it, you don't have to write it to the file system.

Actually, on the argument about where to store it. If you have SQL server 2008, you want to use that. It will store the file on the file system, but you can access it through the database like you would with any other data. You get the best of both worlds.

Upvotes: 1

Related Questions