asp.net下向数据库存储和读取图片示例

 

此页面是用户上传与显示数据库图片的界面。

ContractedBlock.gif ExpandedBlockStart.gif Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>無題のページ</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
&nbsp; &nbsp;
        
<asp:FileUpload ID="FileUpload1" runat="server" />&nbsp;
        
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><br />
        
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
        
<asp:Image ID="Image1" runat="server" />
        
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" /></div>
    
</form>
</body>
</html>
ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using System.Data.SqlClient;
using System.Web.Configuration;
using System.IO;
using System.Web.Hosting;

public partial class _Default : System.Web.UI.Page
{

    
protected void Button1_Click(object sender, EventArgs e)
    {
        BinaryReader br 
= new BinaryReader(this.FileUpload1.FileContent);

        
if (br.BaseStream.Length == 0)
        {
            Label1.Text 
= "File size should not be zero!";
            
return;
        }

        SqlConnection con 
= new SqlConnection(WebConfigurationManager.ConnectionStrings["testDB"].ConnectionString);
        SqlCommand comm 
= new SqlCommand();
        comm.Connection 
= con;
        comm.CommandText 
= "INSERT INTO dbo.picture VALUES (@picID,@bin)";

        Guid g 
= Guid.NewGuid();
        comm.Parameters.Add(
"@picID", SqlDbType.UniqueIdentifier).Value = g;
        comm.Parameters.Add(
"@bin", SqlDbType.Image).Value = br.ReadBytes((int)br.BaseStream.Length);

        con.Open();
        comm.ExecuteNonQuery();
        con.Close();

        Label1.Text 
= g.ToString();

    }
    
protected void Button2_Click(object sender, EventArgs e)
    {
        Image1.ImageUrl 
= "CreateImage.aspx?picID=" + Label1.Text;
    }
}

 

此页面是用来读取数据库的二进制数据,生成数据流提供给主页面Image1.ImageUrl属性。

ContractedBlock.gif ExpandedBlockStart.gif Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateImage.aspx.cs" Inherits="CreateImage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>無題のページ</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
    
</div>
    
</form>
</body>
</html>
ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
using System.Web.Configuration;
using System.IO;
using System.Web.Hosting;

public partial class CreateImage : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {

        Guid picID;
        
if (Request.QueryString["picID"!= null || Request.QueryString["picID"!= "")
        {
            picID 
= new Guid(Request.QueryString["picID"]);
        }
        
else
        {
            picID 
= new Guid();
        }

        SqlConnection con 
= new SqlConnection(WebConfigurationManager.ConnectionStrings["testDB"].ConnectionString);
        SqlCommand comm 
= new SqlCommand();
        comm.Connection 
= con;
        comm.CommandText 
= "SELECT * FROM dbo.picture WHERE picID =@picID";
        comm.Parameters.Add(
"@picID", SqlDbType.UniqueIdentifier).Value = picID;

        SqlDataAdapter sda 
= new SqlDataAdapter(comm);

        DataTable dt 
= new DataTable("picture");

        con.Open();
        sda.Fill(dt);
        con.Close();

        
if (dt.Rows.Count> 0)
        {
            Response.ContentType 
= "image/jpeg";
            Response.BinaryWrite((
byte[])dt.Rows[0]["bin"]); 
        }
    }
}

转载于:https://www.cnblogs.com/WayToDotNET/archive/2009/02/09/1386736.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值