Home All Groups Group Topic Archive Search About

Retreive images stored in Access database

Author
24 Nov 2005 12:24 PM
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

Author
24 Nov 2005 2:52 PM
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
Are all your drivers up to date? click for free checkup

Author
23 Dec 2005 1:29 PM
B
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

Show quoteHide quote
"Wouter van Vugt" wrote:

> 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
>
>

Bookmark and Share