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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

16 Experts available now in Live!

Get 1:1 Help Now