Storing and retrieving dynamically created pdf in sql - c#

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?

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?

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.

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.

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

You can use a table like this to store files in SQL Server.
CREATE TABLE [Documents]
(
[FileName] nvarchar(1000),
[FileContent] varbinary(max)
)

You have 2 ways to do that:
Store in FileServer and store the Filename in the database.
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!!!!!

Related

General solution for saving and retrieving different files in sql server

I know how to store images to db (convert them to byte[] and then save it) and also for retrieving (select byte[] from db and use image methods to create image from byte[]). I'm cool so far, but how can I save/retrieve a PDF to database? What about .doc , .mp3 , .exe and say .ppt files?
Is there a general way to save and retrieve files to and from sql server? The worst part is retrieving, let's imagine we found a way to save any file to sql server, now how can we rebuild the file from db? We don't know what the file extension was before saving?
Well, Generally speaking, it's considered bad practice to save actual files to the database.
a part of the reason is the problems you mentioned in your post, and an even bigger part is that saving files directly to the database has a large overhead (such as translating an image to a byte array and back).
the easy (and recommended in most cases) way to handle files and databases is to save the files directly to the file system, and keep the path in the database along with other file-related data such as the user id that uploaded the file.
this way you don't need to worry about braking and rebuilding the files, you just send them to the server and back to the user as is.
Keep in mind it's not recommended to keep the full path of the file, only a relative path.
What I normally do is save all files from the users either on the serer itself or on the users's computer (in a desktop application). in any of these cases, there is a dedicated folder with only read/write permissions (NEVER let a user save a file into a directory with execute permissions!), and keep the path of this directory either on a 'General Params' table in the database or in the configuration file of the website / application / webservice.
Well, the file attributes (name, extension, author, etc) are usually kept in relational way, in table inside SQL Server. The file itself should be kept in SQL Server database, exactly how depends on version od SQL Server and size of file. Use FILESTREAM or FILETABLES feature for larger, or VARBINARY(MAX) for smaller files.
It doesn't matter whether its an image, or doc or pdf -- if you car read into a FileStream, you can save it to database.
Advantages of storing files in a database is simplified management, backups, security, integrity. With FILESTREAM and FILETABLES feature, accessing a file is almost the same as if it were on a file system, using the SqlFileStream object from .NET.
See more here:
http://technet.microsoft.com/en-us/library/gg471497.aspx
And here:
http://technet.microsoft.com/en-us/library/ff929144.aspx

Storing references to images in SQL Server using ASP.NET and C#

I am trying to store references of images in a SQL Server DB and store the actual images in a file server/folder. I am hoping someone could give me a link or code example on how to do this. I don't want to store BLOB in the database.
I am using ASP.NET/C# to handle this.
All files in a folder have unique names, so I think you shouldn't worry about storing the path of the image, as suggested in the comments. If you are worried about consistence, i.e. someone deleting a "referenced" image or inserting a path to nonexistent image file, you could check that either from your application, or even from the database itself.
However I would not hesitate to use a blob, you can use MS SQL 2012 and insert the image files to a file table, which sounds quite convenient.
as per my knowledge.or my expirence the images are stored in sql server that is in image datatype feild.it is stored in byte format.that is actlly the reference of the actual image.hope this link help you to get more clear idea about it
http://www.sqlhub.com/2009/03/image-store-in-sql-server-2005-database.html
http://www.codeproject.com/Articles/10861/Storing-and-Retrieving-Images-from-SQL-Server-usin
Use a HttpHandler to grab the image from the database and use the Image data type:
Retrieve image of image control as byte array which is set using generichandler(.ashx)?
Storing images in your dataase or in a filestream, totaly depends on your images size. In Microsoft Research there is a good paper called To BLOB or Not To BLOB.
After a lot of test and much analysis;
If your pictures are below 256K, store them in datebase VARBINARY column is good.
If your pictures are over 1 MB, storing them in the filesystem is good.(With FILESTREAM attribute, they are still under transactional control and part of the your database)

Store pdf files in a database

I would like to design a C# application to store emlployees data, I have around 500 employees. I want to store also pdf scanned profile of each employee. I am planing to use PostgreSQL. Is it practical to store the pdf scanned profiles in the database? Do I need to use blob data-type?
Assuming that PDF is not going to be very large (probably less than 5MB I assume) it is ok. You should use type BYTEA for this.
Read more about how to use Npgsql: .NET Postgresql driver (scroll to Working with binary data and bytea datatype)
yes you need to save them as BLOB objects or in bytea or text types, and you need to consider Postgres limitation regarding this. limited 2G's per entry, & 4 Billion per database for blobs and limited to 1G per entry,4 Billion entries per table for bytea or text, but if i were you i will save a reference to this file in the database "where this PDF is located in the local file system" and you stream this file once it is needed
for PostgreSQL limitation check the following link http://wiki.postgresql.org/wiki/BinaryFilesInDB

Saving file to MySQL Database

I am working on a c# project with a MySQL database. I have a log file and I want to store this file in the database.
I want to save the actual file into the database but what I've seen is you have to encode it to a string or write the bytes to a string and put that in the database, is that how it works to store the file, I thought you could just give the command the file path and it stores it in the database without me requiring to do all the encoding in the software.
If this makes any difference, it will need to be retrieved again from PHP, but I'm guessing that this shouldn't matter.
Yes you have to encode it to string or bytes to store the file. Another way would be store the log file location, and instead save the log file on the disk some where.
This may sound a little ludicrous but you should look into the LOAD_FILE function
Here is the example for the MySQL Documentation
mysql> UPDATE t
SET blob_col=LOAD_FILE('/tmp/picture')
WHERE id=1;
As long as the log file is small enough to fit into a BLOB, then that's your only shot at it
If the log file is too big, just archive the log file somewhere and save the file location instead
Your program is going to have to parse the log file and turn it into an SQL query.
If you want to store the contents of the file in the database, then yes, you either encode it or store the bytes in the table column. There's no way around that.
If you want to simply store the path to the file, then all you need to do is pass in the path as parameter to your MySQLCommand but the moment you move the file around in the filesystem, your record will be invalid, obviously.
Link to a somewhat related question regarding which approach is best: Storing the content of the file in the database or simply the path in the file system.
Why not use Log4Net? It has the ability to log to databases automatically. Take a look at https://logging.apache.org/log4net/release/config-examples.html for details on how to connect a database to Log4Net
orignal decayed link: http://logging.apache.org/log4net/release/sdk/log4net.Appender.AdoNetAppender.html

Save document in SQL Server database

I have a C# / SQL Server project. and database is reachable from different places (no lan between that 3 places) and data in database is important so I am taking recovery or my database every hour for last 30 days.
Documents which I want to save are kind of fax, excel, word, pdf type data and not formatted. So its impossible to get data inside them.
Problem is how can I store documents in SQL Server I don't want to enlarge its size so much because of increasing backup size.
So what is the efficient solution?
It seems like your main issue is the size of your backup. If you are doing a full backup every hour then you could save space by doing a differential backup instead.
There is no need to backup everything if it hasn't all changed, so you would only need to backup the new data that hadn't been in the last backup.
This would save you a lot of space and time and is generally better practice.
I would suggest you consider implementing a backup rotation scheme. You can find more information on this here:
http://en.wikipedia.org/wiki/Backup_rotation_scheme
I would also suggest you save the file in the filestream data type field in order to reduce the performance impact of having large pages in the mdf file.
If you want to store something it's going to take place. You have multiple choises:
Store only file path in SQL and store files seperatly on server and have seperate backup process for them
Compress files before putting them to sql server, it will save you some place especialy with plain text formats, though it won't help with allready compressed formats(.png, office .docx, .xlsx and so on)
Use FILESTREAM and differential backups (Example)
Similar question: Store Files in SQL Server or keep them on the File Server?
If you worries about backups size - save documents in filesystem and in DB store only patches.
If you worries about backups consistency - store documents inside the DB

Categories