Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 65,918 times

Contents

Related Categories

Retrieving Images from a Database - Getting the Data

dave123aspx

Getting the Data

Now that we know the structure of our image table, lets begin by getting the data out of the database.

//get the image id from the url
string ImageId = Request.QueryString["img"];

//build our query statement
string sqlText = "SELECT img_data, img_contenttype FROM Image WHERE img_pk = " + ImageId;

SqlConnection connection = new SqlConnection( ConfigurationSettings.AppSettings["DSN"].ToString() );
SqlCommand command = new SqlCommand( sqlText, connection);

//open the database and get a datareader
connection.Open();
SqlDataReader dr = command.ExecuteReader();

We start by finding the "ID" of the image we want. The "ID" is really the img_pk field, but we are hiding the real name from our users. Once we have the primary key of the image we want, we write our select statement, sqlText. An example URL would be http://localhost/viewimage.aspx?img=1. Once we have the image ID (or img_pk) we build our Sql Statement and store it in a local variable called sqlText. We define and open a connection to the database using a SqlConnection and use a SqlCommand to execute our sqlText. By executing our SqlCommand, we return a SqlDataReader. We chose a datareader, because of it's speed and low overhead.

Comments

  • Re: can't get this to work

    Posted by manoj3939 on 18 Apr 2007

    I am Facing the sam e problem, i am able to store an image into a database as image type but fetching from database in displaying it on a web page is not working.


    Fetching from the da...

  • Posted by tomk10 on 14 Dec 2005

    Whar is Bind? I've only seen DataBinder.Eval used here and your examp,le causes a Compilation Error.

  • can't get this to work

    Posted by Jud101 on 25 Nov 2005

    I've been struggling to get an image to view on my webpage. I had to make a few changes but no way would it work for me. I ended up coping and pasting everything, ran into the error at this point : ...

  • Fix

    Posted by Louie88 on 15 Nov 2005

    Hi there! Try this „quite” simple fix. Thanks Dave, you helped a lot with this images mystery…

    ...

  • Same Problem

    Posted by tadhg88 on 22 Feb 2005

    hey guys i have the exact same problem as ye have and im just wondering if ye managed to find a solution
    thanks in advance
    tadhg88