Hi,
i have followed a tutorial and modified it to get a image stored as a blob from MSSQL, it is working perfectly - however i utilised handlers in this tutorial. and while it is working fine - id quite like to understand what is going on!
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Collections.Specialized;
public class Handler : IHttpHandler {
public string GetConnectionString()
{ // i think i know this bit gets connection string from web.config!
return System.Configuration.ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
}
public void ProcessRequest(HttpContext context)
{ //what does this context.request mean?
string id = context.Request.QueryString["CarID"];
if (id != null)
{
try
{
//creates new memory stream for the binary image data i guess!
MemoryStream memoryStream = new MemoryStream();
//create new sql connection and sql command
SqlConnection connection = new SqlConnection(GetConnectionString());
string sql = "SELECT * FROM Car WHERE CarID = @CarID";
//creates new sql command
SqlCommand cmd = new SqlCommand(sql, connection);
//add paramaters to command
cmd.Parameters.AddWithValue("@CarID", id);
//open connection
connection.Open();
//what does this do and why is it needed?
SqlDataReader reader = cmd.ExecuteReader();
//im guessing this has something to do with the datareader
reader.Read();
//byte array for storing the image i guess
byte[] file = (byte[])reader["Image"];
//close the reader and connection
reader.Close();
connection.Close();
//does this write to the array?
memoryStream.Write(file, 0, file.Length);
//i have no idea what these 2 do
context.Response.Buffer = true;
context.Response.BinaryWrite(file);
//dispose of memory stream? :p lol
memoryStream.Dispose();
}
catch
{
}
}
}
//set to false - what is it? - i think its false because of something to do with threading
public bool IsReusable {
get {
return false;
}
}
}
if anyone could also tell me the order of whats going on that would be most appriciated :) like i say ive got it working fine - but what good is following tutorials if i dont understand it!
thanks guys xx :)