1

How to retrieve images from SQL Database using C#?

5
  • 3
    Google "Display Blob Image C#" Commented Mar 31, 2011 at 9:47
  • What's your approach? Why doesn't it work (Exception message, compiler error...)? Commented Mar 31, 2011 at 9:48
  • Go ahead,give it a try first, comeback here if you're stuck somewhere! Commented Mar 31, 2011 at 9:51
  • Bare in mind sql stores an image as a blob, which is essentially a byte array. Try and look for examples on converting a byte array to a c# Image object. Commented Mar 31, 2011 at 9:55
  • SO won't let me answer with a let me google that for you but if you use your exact question as a google search you get your answer. Commented Mar 31, 2011 at 9:56

2 Answers 2

2

I believe this is what you need: an msdn example with code! it should at least get you started.

Sign up to request clarification or add additional context in comments.

Comments

0

Why dont you try by using query string . Here is my code where in query string is used and i fetch images from the database without having to create the any instatnce of that desired image inyour local folder. Hop this helps.

<%@ WebHandler Language="C#" Class="DisplayImg" %>

using System;
using System.Web;
using System.Configuration;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public class DisplayImg : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string theID;
        if (context.Request.QueryString["id"] != null)
            theID = context.Request.QueryString["id"].ToString();
        else
            throw new ArgumentException("No parameter specified");

        context.Response.ContentType = "image/jpeg";
        Stream strm = DisplayImage(theID);
        byte[] buffer = new byte[2048];
        int byteSeq = strm.Read(buffer, 0, 2048);

        while (byteSeq > 0)
        {
            context.Response.OutputStream.Write(buffer, 0, byteSeq);
            byteSeq = strm.Read(buffer, 0, 2048);
        }
    }

    public Stream DisplayImage(string theID)
    {
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SERVER"].ConnectionString.ToString());
        string sql = "SELECT Server_image_icon FROM tbl_ServerMaster WHERE server_Code = @ID";
        SqlCommand cmd = new SqlCommand(sql, connection);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@ID", theID);
        connection.Open();
        object theImg = cmd.ExecuteScalar();
        try
        {
            return new MemoryStream((byte[])theImg);
        }
        catch
        {
            return null;
        }
        finally
        {
            connection.Close();
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Just add one line in CS code

UploadImg.ImageUrl = "~/DisplayImg.ashx?id=" + code;

1 Comment

-1 for not using using, not null checking, and returning a stream from the method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.