?
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
Medium Priority
?
484 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
[X]
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
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
kris_per earned 1332 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 668 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 1332 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 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