Click here to Skip to main content
15,902,939 members
Articles / Programming Languages / C#
Article

Word, Excel, Image, XML Files Stroe / Retrive from Database

Rate me:
Please Sign up or sign in to vote.
2.10/5 (15 votes)
4 Apr 2007CPOL 92.6K   2K   52   16
This article will explian store / retrive the word documents, excel files, Images and xml to Sql server database.

Sample Image - FileStore2DataBase.jpg

Introduction

In this code I used stream objects for converting the documents and images into byte objects. These byte objects will be stored in the database table in the image field.

How to convert images and documents into byte objects in C#

Here is the simple code to convert these files into byte objects.

C#
FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, 
    FileAccess.Read);
long len;
len = fileStream.Length;
Byte[] fileAsByte = new Byte[len];
fileStream.Read(fileAsByte, 0, fileAsByte.Length);
MemoryStream memoryStream = new MemoryStream(fileAsByte);
return memoryStream.ToArray();

Here fileName represents the name and location of the file. I used fileStream objects to read the files and convert these files into byte objects using the fileStream.Read command. I invoked memorystream to convert the bytes into byteArray.

How to convert the byte objects into words and images from C#

C#
FileStream fs = new FileStream(di + "\\" + fileName, FileMode.Create);
fs.Write(content, 0, System.Convert.ToInt32(content.Length));
fs.Seek(0, SeekOrigin.Begin);
fs.Close();

The above code explains how the images/documents are converted to byte objects.

How to Store these files into a Database

SQL
SqlCommand insert = new SqlCommand("insert into FileStore 
    _([FileName],[FileStream]) values (@FileName,@FileStream)", 
    _sqlConnection);
SqlParameter imageParameter = insert.Parameters.Add("@FileStream", 
    _SqlDbType.Binary);
SqlParameter fileNameParameter = insert.Parameters.Add("@FileName", 
    _SqlDbType.VarChar);
imageParameter.Value = content;
imageParameter.Size = content.Length;
fileNameParameter.Value = fileName;
insert.ExecuteNonQuery();

The above code will explain how the content of the image/document is stored into the database.

Database Table Design:

  • Table Name: FileStore
  • Field:
    FileId int - set its identity field.
  • fileName Varchar(50)
  • FileStream image

Summary:

The program was developed by C#.NET 2.0 and SQL Server 2005.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
Hi,
I have 6+ years of experience in Microsoft Technologies and have 2+ years of experience in .NET Technologies. Currenty I am woring in Hyderabad and involed in various domain projects.

If you have any doubts please reach me: rajaramanindia@gmail.com

Comments and Discussions

 
Questiongetting error at the time of open file Pin
Member 980964228-Mar-13 22:39
Member 980964228-Mar-13 22:39 
GeneralFile Length Problem Pin
gulkar17-Dec-10 23:01
gulkar17-Dec-10 23:01 
GeneralWord file retrieve from database Pin
gulkar17-Dec-10 22:50
gulkar17-Dec-10 22:50 
QuestionCan i re-save a file with changes? Pin
Abdullah...23-Jun-07 22:37
Abdullah...23-Jun-07 22:37 
Questionencoded image problem xsl Pin
ksrs20-Jun-07 3:05
ksrs20-Jun-07 3:05 
GeneralA problem from chiragshah4885 Pin
Sean Ewington4-Apr-07 4:32
staffSean Ewington4-Apr-07 4:32 
GeneralRe: A problem from chiragshah4885 Pin
Rajaraman.net054-Apr-07 5:34
Rajaraman.net054-Apr-07 5:34 
GeneralProblem with c# code Pin
GarethEvans811-Mar-07 7:08
GarethEvans811-Mar-07 7:08 
GeneralRe: Problem with c# code Pin
Rajaraman.net0528-Mar-07 23:39
Rajaraman.net0528-Mar-07 23:39 
Questionencoded image problem xslt to excel Pin
ksrs20-Jun-07 3:08
ksrs20-Jun-07 3:08 
GeneralMemoryStream unnecessary Pin
Tommy Carlier10-Dec-06 20:30
Tommy Carlier10-Dec-06 20:30 
GeneralRe: MemoryStream unnecessary Pin
sea0920-Jan-11 16:25
sea0920-Jan-11 16:25 
GeneralBroken Link Pin
mmansf10-Dec-06 3:14
mmansf10-Dec-06 3:14 
Link to download the "Source Files" is broken. Also, is there a "Demo" to see exactly how to implement this
AnswerRe: Broken Link Pin
Rajaraman.net0510-Dec-06 19:02
Rajaraman.net0510-Dec-06 19:02 
GeneralRe: Broken Link Pin
Chris Maunder11-Dec-06 21:10
cofounderChris Maunder11-Dec-06 21:10 
GeneralRe: Broken Link Pin
Rajaraman.net0512-Dec-06 2:10
Rajaraman.net0512-Dec-06 2:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.