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
463 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MVC5 Dataformat String for Phone not working 3 25
bulid json format 3 46
New to SOAP and need some direction 10 46
About delegates in c sharp 3 35
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now