nnmmss
nnmmss

Reputation: 2974

Storing files in SQL Server database

I want to store files in my SQL Server database by C# which I have done it without problem.

This is my code:

 byte[] file;

 using (var stream = new FileStream(letter.FilePath, FileMode.Open, FileAccess.Read))
 {
      using (var reader = new BinaryReader(stream))
      {
         file = reader.ReadBytes((int)stream.Length);
         letter.ltr_Image = file;          
      }
 }

 LetterDB letterDB = new LetterDB();
 id = letterDB.LetterActions(letter);

The insert SQL action in the LetterActions module. But I want to know, in order to reduce the size of the database (which increases daily) is there any solution for compressing the files and then store them in the database?

Upvotes: 1

Views: 2705

Answers (2)

Bucketcode
Bucketcode

Reputation: 481

You can compress file like this. Then insert compressed file stream into DB, but when you read it you need decompress it.
If you really need store file in DB, suggest you compress and decompress it by client.
And better way handle file is store them in disk, and only store file path in DB, when client need file use file path get file.

Upvotes: 1

chrisl08
chrisl08

Reputation: 1658

Yes , you can zip your files before storing them in the database, using the ZipFile class. Take a look here: https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile(v=vs.110).aspx

Plenty of sample code out there too. See here:http://imar.spaanjaars.com/414/storing-uploaded-files-in-a-database-or-in-the-file-system-with-aspnet-20

Upvotes: 1

Related Questions