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