Retreive images stored in Access database

A

Anitha

Hi All,

How to retrieve images stored in Access database.
I am storing images(jpeg) as OleObject.
I want display them on my web page.
I am unable to do so.Please help me I am using C#
The code is as

// Put user code to initialize the page here
int ImgID = System.Convert.ToInt32(Request.QueryString["ImgID"]);
System.Data.OleDb.OleDbConnection Con = new
System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\images.mdb;");
System.String SqlCmd = "SELECT Image FROM Images";
System.Data.OleDb.OleDbCommand OleDbCmdObj = new
System.Data.OleDb.OleDbCommand(SqlCmd, Con);
Con.Open();
System.Data.OleDb.OleDbDataReader OleReader =
OleDbCmdObj.ExecuteReader();


while(OleReader.Read())
{
Response.ContentType = "IMAGE/JPEG";

Response.BinaryWrite( (byte[])OleReader["Image"]);
}

Response.End();



Tahnks,
Anitha
 
W

Wouter van Vugt

Hi Anitha,

you'll have to use a custom HTTP Handler for this (or use a separate
dedicated ASPX, easier but less nice). In your code I notice you are
using a while loop to read images. Don't forget you can only serve 1
image, as the client is only requesting a single image
(http://mysite/myimage.jpg).

namespace SharpCMS.Core.HttpHandlers
{
using System;
using System.Web;
using System.Data;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
using SharpCMS.Schema;
using SharpCMS.Core.Utils;

public class ResourceHandler
: IHttpHandler
{
public bool IsReusable
{
get { return false; }
}

public void ProcessRequest(HttpContext context)
{
string resourceID = context.Request.QueryString["resID"];
Guid guid = new Guid(resourceID);
if (guid != Guid.Empty)
{
FileResource resource = ResourceManager.GetFileResource(new
Guid(resourceID));
context.Response.ContentType = resource.MimeType;
using (BinaryWriter writer = new
BinaryWriter(context.Response.OutputStream))
{
writer.Write(ResourceManager.GetFileBytes(resource.ID));
}
}
context.Response.End();
}
}
}

This code is taken from my CMS'ish application, SharpCMS. The call to
ResourceManager.GetFileBytes does nothing more than your select. I use
Sql Server and an column of type Image. The SqlCommand returns a byte[]
for this.

Hope it helps,

Grtz, Wouter
Trainer - Info Support - www.infosupport.com
www.dive-in-it.nl
 
G

Guest

Hey Wouter,

Do you have the same code in VB.NET ?
I would like to retreive my images as well from SQL 2005 Express DB.

Thanks,
Bart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top