Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

What's the best way to display an image from a sql server database in asp.net?

I have a sql server database that returns byte for the image. If I use the tableadapter wizard and set it to my stored procedure and preview data, it pulls back an image. It automatically turns it into an image in the preview data. I don't see it as a string of Ints or anything.

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

I have searched and foudn where the imagefield can point to a url on another page that does the byte transformation but I'm not sure it's the best. I found another way that creates a temp file.

Just trying to see the best way to do it.

edit - I am trying not to use a temp file. If I cannot use a gridview a regular image field is ok.

asp.net 2.0, c#.

Thank you for any help.

edit

ended up with:

   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 on the calling page:

<asp:Image ID="Image1" ImageAlign="Middle" ImageUrl="show.aspx?id=123" Runat="server" />
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Two options:

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

Serve it from another URL - This is my preferred method, as you have no disk access required. A simple http handler (ashx) is a great method to serve up the image.

Edit

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

Edit

Couple more thoughts. There are some cases where using a temp file might be better. For example if your images are requested frequently by a lot of users. Then storing the images on the disk would make sense, since you could write the file once, this does increase the maintance complexity but depending on traffic it might be worth it since this would let you avoid calling back into the .net stack and leverage IIS caching of static content.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...