读取并显示数据库中的二进制图片(一张和一组)
2009-07-21 10:55
459 查看
1,一张需两个.aspx页面,分别为1.aspx,2.aspx。
1.aspx的后台代码为
2.仅需前台代码就可:
二,一组图片上传只需1.aspx一个页面的后台代码,和一个一般处理程序Getimg.ashx代码分别是:
1.aspx的后台代码为
<asp:Image ID="Image1" runat="server" Height="258px" Width="177px" /> protected void Page_Load(object sender, EventArgs e) { Image1.ImageUrl = "Register.aspx"; }
2.仅需前台代码就可:
public partial class Register : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string imgid = Request.QueryString["imgid"]; SqlConnection conn1 = new SqlConnection("Data Source=localhost;Initial Catalog=S601;User ID=sa;Password=123456"); SqlCommand cmd1 = new SqlCommand("select picture,pname,psize,pid from pictures where pid=1", conn1); conn1.Open(); SqlDataReader sdr = cmd1.ExecuteReader(); if (sdr.Read()) { Response.BinaryWrite((byte[])sdr["picture"]); } Response.End(); }
二,一组图片上传只需1.aspx一个页面的后台代码,和一个一般处理程序Getimg.ashx代码分别是:
<asp:DataGrid id="MyDataGrid" runat="server" AutoGenerateColumns="False" Width="632px"> <AlternatingItemStyle BackColor="Beige"></AlternatingItemStyle> <HeaderStyle HorizontalAlign="Center"></HeaderStyle> <Columns> <asp:TemplateColumn HeaderText="Photo"> <ItemTemplate> <asp:Image ID="Image1" runat="server" Height="70px" ImageUrl='<%# "Getimg.ashx?id="+Eval("pid") %>' Width="100px" /> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:S601ConnectionString %>" SelectCommand="SELECT pid, pname, psize, picture, ptype FROM pictures"></asp:SqlDataSource>
<%@ WebHandler Language="C#" Class="Getimg" %> using System; using System.Web; using System.Data; using System.Data.SqlClient; using System.Configuration; public class Getimg : IHttpHandler { public void ProcessRequest (HttpContext context) { int id = int.Parse(context.Request.QueryString["id"]); SqlConnection conn = new SqlConnection(@"Data Source=localhost;Initial Catalog=S601;User ID=sa;Password=123456"); SqlCommand cmd = new SqlCommand("select picture from pictures where pid='" + id + "'", conn); cmd.Parameters.Add("@id",SqlDbType.Int).Value=id; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { context.Response.BinaryWrite((byte[])dr["picture"]); } dr.Close(); } public bool IsReusable { get { return false; } } }
相关文章推荐
- 上传图片以二进制插入到数据库,且读取显示
- 从数据库中读取二进制数据,并显示图片
- 上传图片以二进制插入到数据库,且读取显示
- 上传图片以二进制插入到数据库,且读取显示
- 上传图片以二进制插入到数据库,且读取显示
- 上传图片以二进制插入到数据库,且读取显示
- 上传图片以二进制插入到数据库,且读取显示
- 数据库读取二进制图片显示到PictureBox中
- ASP.net:上传图片以二进制插入到数据库,且读取显示
- 读取并显示以二进制的格式存储到数据库中图片
- 数据库读取二进制图片显示到PictureBox中
- 上传图片以二进制插入到数据库,且读取显示
- C#从数据库中读取二进制数据,并显示图片
- OpenCV 中读取一张图片的像素点,并显示
- ASP.NET 上传图片至数据库并读取图片显示
- Asp.net中图片存储数据库以及页面读取显示通用方法详解
- C#保存图片到数据库,读取图片显示
- 如何读取保存在数据库中的图片显示在DataGrid中
- NET二进制图片存储与读取数据库的常见方法
- 图片保存到数据库和从数据库读取图片并显示(c#)