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
Solved

Is it possible to serialize any file with extensions like .exe,.pdf,.mp3,etc,.. into a binary format and then store it in SQL Server using ADO.NET & C#?

Posted on 2010-09-08
5
469 Views
Last Modified: 2012-05-10
I am looking for the code if this is possible.. I have done this for an image. by first converting the image to a byte[] array like so :

public static Byte[] Image2Bytes(Image img)
{
             Byte[] arr = new Byte[0];
             
              using(MemoryStream ms = new MemoryStream())
              {
                       img.Save(ms,System.Drawing.Imaging.ImageFormat.JPEG);                        
              }
              arr = ms.ToArray();
              return arr;
}

using the above method i can convert an Image to binary but i need to convert other types to binary and these are our day-to-day commonly used files.
0
Comment
Question by:starlite551
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
kris_per earned 333 total points
ID: 33626015

Yes. it is possible to store the .exe/.pdf file data in sql server column....to get the bindary data of these files, you can use BinaryReader class:




FileStream fs = File.Open(fileName, FileMode.Open);

BinaryReader binReader = new BinaryReader();

byte[] fileData = new byte[fs.Length];
int count = binReader.Read(fileData , 0, fs.Length);
binReader.Close();
fs.Close();

// now use fileData binary array to save in sql server.

Open in new window

0
 
LVL 12

Assisted Solution

by:w00te
w00te earned 167 total points
ID: 33626031
http://www.mssqltips.com/tip.asp?tip=1489
The (very detailed) code at this link shows you how to use a combination of C# and stored procedures within your SQL server database in order to store and retrieve any file in BLOB (binary) format.  It should work with all extensions I believe.
-w00te
0
 
LVL 12

Expert Comment

by:w00te
ID: 33626046
PS: If you feel like being lazy, jump to the "Add a BLOB from C#" header on that site, though I'd recommend reading through it all, it has some good information.  You should also look into pro's and con's of this, storing that quantity of data in a database can drastically impact performance (you can google that if you're worried about performance of the databse after using binary storage). :)
-w00te
0
 
LVL 16

Assisted Solution

by:kris_per
kris_per earned 333 total points
ID: 33626160

One correction in my BinaryReader code above. The line should be:
BinaryReader binReader = new BinaryReader(fs); // pass filestream fs as param

If you are looking for a way to save the binary data into a table, then following link has a very good example of using Sql classes to insert/read bindary data from table (and it uses BinaryReader as well) =>
http://www.codeproject.com/KB/database/Store_images_in_SQL_Serve.aspx
0
 
LVL 12

Author Comment

by:starlite551
ID: 33626318
Thanks guys It really helped a lot!!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 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