asp.net下向数据库存储和读取图片示例
生活随笔
收集整理的這篇文章主要介紹了
asp.net下向数据库存储和读取图片示例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?
此頁面是用戶上傳與顯示數(shù)據(jù)庫圖片的界面。
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>
???????? ?
????????<asp:FileUpload?ID="FileUpload1"?runat="server"?/>
????????<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>
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;
????}
}
?
此頁面是用來讀取數(shù)據(jù)庫的二進(jìn)制數(shù)據(jù),生成數(shù)據(jù)流提供給主頁面Image1.ImageUrl屬性。
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>
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"]);?
????????}
????}
}
轉(zhuǎn)載于:https://www.cnblogs.com/WayToDotNET/archive/2009/02/09/1386736.html
總結(jié)
以上是生活随笔為你收集整理的asp.net下向数据库存储和读取图片示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 插入ASP代码让网站数据库成为ASP木马
- 下一篇: vs2008中C#3.0语言的新特性