Posted By

rengber on 03/24/08


Tagged

sql image c aspnet blob


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

umang_nine
Bonitarunner
nwpratt


ASP.Net Page to Return an Image from an SQL Blob Query


 / Published in: C#
 

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["ProductCatalogueConnectionString"].ConnectionString;
  4. SqlConnection conn = new SqlConnection(connStr);
  5. string blobId = Request.QueryString["ID"];
  6. if (!string.IsNullOrEmpty(blobId))
  7. {
  8. string cmdText = "select A.blob from Core.Attachment A where A.AttachmentID = '" + blobId + "'";
  9. conn.Open();
  10. SqlCommand cmd = new SqlCommand(cmdText, conn);
  11. SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  12. if (reader.Read())
  13. {
  14. byte[] imgBytes = (byte[])reader["blob"];
  15. Response.ContentType = "image/jpeg";
  16. Response.BinaryWrite(imgBytes);
  17. }
  18. }
  19. }

Report this snippet  

You need to login to post a comment.