How can I display an image from a sql server database in asp.net?

I have a sql server database that returns a byte for an image. If I use the tableadapter wizard and install it in my stored procedure and preview data, it discards the image. It automatically turns it into an image in the preview data. I don’t see threads of ints or anything else in it.

How can I display it on my asp.net webpage using gridview and objectdatasource?

I searched and foudn where the image field may point to a URL on another page that performs byte conversion, but I'm not sure if this is better. I found another way to create a temporary file.

Just trying to see the best way to do this.

edit - I try not to use a temporary file. If I cannot use gridview, the regular image field is fine.

asp.net 2.0, C #.

Thanks for any help.

change

ended:

protected void Page_Load(object sender, EventArgs e) { string id = Request["id"]; string connstr = "DSN=myserver"; OdbcConnection conn = new OdbcConnection(connstr); OdbcCommand cmd = new OdbcCommand("{call mySP (?)}", conn); cmd.CommandType = CommandType.StoredProcedure; // Add the input parameter and set its properties. OdbcParameter parameter = new OdbcParameter(); parameter.ParameterName = "@MyParam"; parameter.OdbcType = OdbcType.VarChar; parameter.Direction = ParameterDirection.Input; parameter.Value = id; // Add the parameter to the Parameters collection. cmd.Parameters.Add(parameter); conn.Open(); OdbcDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { byte[] buffer = (byte[])dr[0]; Response.ContentType = "image/jpg"; Response.BinaryWrite(buffer); Response.Flush(); } } 

and this is on the calling page:

 <asp:Image ID="Image1" ImageAlign="Middle" ImageUrl="show.aspx?id=123" Runat="server" /> 
+4
sql-server image blob
Mar 04 '09 at 20:44
source share
4 answers

Two options:

Create a temporary file. The problem with this approach is that you need to create a file, which means that your web must have write access to a directory that is not great. You also need to have a way to clean your images.

Submit it from a different URL. This is my preferred method since you do not need disk access. A simple HTTP handler (ashx) is a great method for serving an image.

Edit

If you need session state in ashx check: Asp.net System.Web.HttpContext.Current.Session null in global.asax .

Edit

A few more thoughts. There are times when using a temporary file might be better. For example, if your images are often requested by many users. Then saving the images to disk makes sense, since you can write the file once, this increases the complexity of the service, but depending on the traffic it may cost, as this will allow you to avoid getting back to the .net stack and use IIS caching of static content .

+4
Mar 04 '09 at 20:48
source share
β€” -

Here is a sample code on how to do this.

0
Mar 04 '09 at 20:46
source share

The sample code you added is good, but you have to transfer it to the .ashx file, which is designed for such things.

0
Mar 04 '09 at 22:56
source share

I wrote the SqlReader plugin for the open source ImageResizing.NET library so that you can display and display images from an SQL database in the most optimal way.

Even if you do not need to do any image processing, it is still (a) the easiest and (b) the most efficient way to do this. You can combine it with disk caching (which provides automatic cleaning) to get maximum performance.

Installation is simple - 2 nuget commands, or copy and paste into Web.Config, your choice.

If you need help, support is free and fast.

0
Dec 01 '11 at 3:21
source share



All Articles