Click here to Skip to main content
Licence CPOL
First Posted 10 Dec 2006
Views 57,833
Downloads 906
Bookmarked 52 times

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

By | 4 Apr 2007 | Article
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.

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#

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

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)

About the Author

Rajaraman.net05



India India

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralFile Length Problem Pinmembergulkar23:01 17 Dec '10  
GeneralWord file retrieve from database Pinmembergulkar22:50 17 Dec '10  
QuestionCan i re-save a file with changes? PinmemberAbdullah...22:37 23 Jun '07  
Questionencoded image problem xsl Pinmemberksrs3:05 20 Jun '07  
GeneralA problem from chiragshah4885 PinstaffSean Ewington4:32 4 Apr '07  
GeneralRe: A problem from chiragshah4885 PinmemberRajaraman.net055:34 4 Apr '07  
GeneralProblem with c# code PinmemberGarethEvans817:08 1 Mar '07  
GeneralRe: Problem with c# code PinmemberRajaraman.net0523:39 28 Mar '07  
Questionencoded image problem xslt to excel Pinmemberksrs3:08 20 Jun '07  
GeneralMemoryStream unnecessary PinmemberTommy Carlier20:30 10 Dec '06  
GeneralRe: MemoryStream unnecessary Pingroupsea0916:25 20 Jan '11  
GeneralBroken Link Pinmembermmansf3:14 10 Dec '06  
AnswerRe: Broken Link PinmemberRajaraman.net0519:02 10 Dec '06  
GeneralRe: Broken Link PinadminChris Maunder21:10 11 Dec '06  
GeneralRe: Broken Link PinmemberRajaraman.net052:10 12 Dec '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 4 Apr 2007
Article Copyright 2006 by Rajaraman.net05
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid