Store and Display Images from MS Access Database Using C#

Updated on 27 Nov 2008, Published on 05 Mar 2008

There is only the difference of coding syntax to stream the binary data of image content type to upload it into MS Access Database using C#/VB in ASP.Net 2.0

 

Namespaces required:

 

using System.Data.OleDb;

OleDb is used to connect the web site forms with MS Access Database using Microsoft.Jet.OLEDB.4.0

 

using System.IO;

IO is used to create the memory stream variable that can store the binary format of the image content.

 

C# Code to Upload Image to MS Access Database:

 

int imageSize;

string imageType;

Stream imageStream;

// Gets the Size of the Image

imageSize = fileImgUpload.PostedFile.ContentLength;

 

// Gets the Image Type

imageType = fileImgUpload.PostedFile.ContentType;

 

// Reads the Image stream

imageStream = fileImgUpload.PostedFile.InputStream;

 

byte[] imageContent = new byte[imageSize]; int intStatus;

intStatus = imageStream.Read(imageContent, 0, imageSize);

 

Connection string to connect the ASP.Net 2.0 web application with MS Access Database:

 

// Access Database oledb connection string

// Using Provider Microsoft.Jet.OLEDB.4.0

String connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("App_Data/db1.mdb");

 

Download the free source code in ASP.Net 2.0 using C#:

AccessImagesCSharp.zip (124.18 kb)

AccessImagesCSharp.rar (71.59 kb)

Product Spotlight

Share it
Rate this article:
Email it:
email

8 Responses to "Store and Display Images from MS Access Database Using C#"
Pratik
This site satisfy my requirment with good codding.
If u have this king of codding please send me on my Email ID.
Thanks..........
premkumar
hi, it is usefull when developing projects
Archit Shrivastav
really useful post
laxman
how to store imagesize in database
Top54u
Hi Laxman
If you want to save imagesize in bytes then first of all create a new field img_size as number in MS Access database to store the imagesize.
Then modify the insert query by adding the new img_size field along with its parameter (@img_size) to set its value.
You can download the sample code provided with this article and apply the changes as above.
Iqa
Why on my program the reference of using system.data.oledb cannot be called upon
Hi Iqa

What error message you are getting while executing the web page?
Thank you so much for this!! I am currently a student and have been through 200 level programming classes....but never learned how to pull and convert binary image data from a database. I want to add this to my personal site and your code works fine. Thanks so much!
Leave a Reply

Name   (Required)


Mail   (will not be published) (Required)


Website   (http://www.example.com)




OR Subscribe via Email:

Programming Ezine rss feed
Top54u Ezines