We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

rengber on 03/24/08


Tagged

sql image c aspnet blob


Versions (?)


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.