Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
490 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

618 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