此页面是用户上传与显示数据库图片的界面。
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>
<%@ 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;
}
}
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属性。
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>
<%@ 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"]);
}
}
}
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"]);
}
}
}