您的位置:首页 > 数据库

Asp.net 2.0(C#)图片存储到数据库和从数据库读取显示

2007-01-18 16:47 871 查看
最近捣鼓了一下,效果就是这样:
图1:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImageFileUpload.aspx.cs" Inherits="ImageFileUpload" %>

<!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="Form2" enctype="multipart/form-data" runat="server">

<asp:Table ID="Table1" Runat="server" Width="39%" BorderWidth="1" BackColor="Beige">

<asp:TableRow runat="server">

<asp:TableCell ColumnSpan="2" BackColor="Red" runat="server">

<asp:Label ID="Label1" Font-size="12px" ForeColor="White" font-bold="True" Runat="server" Text="添加" Font-Names="verdana" />

</asp:TableCell>

</asp:TableRow>

<asp:TableRow runat="server">

<asp:TableCell HorizontalAlign="Right" runat="server"><asp:Label ID="Label2" Font-size="12px" Runat="server" Text="名字" Font-Names="verdana" /></asp:TableCell>

<asp:TableCell runat="server"><asp:TextBox id="txtPersonName" Runat="server" /></asp:TableCell>

</asp:TableRow>

<asp:TableRow runat="server">

<asp:TableCell HorizontalAlign="Right" runat="server"><asp:Label ID="Label3" Font-size="12px" Runat="server" Text="Email" Font-Names="verdana" /></asp:TableCell>

<asp:TableCell runat="server"><asp:TextBox id="txtPersonEmail" Runat="server" /></asp:TableCell>

</asp:TableRow>

<asp:TableRow runat="server">

<asp:TableCell HorizontalAlign="Right" runat="server"><asp:Label ID="Label4" Font-size="12px" Runat="server" Text="性别" Font-Names="verdana" /></asp:TableCell>

<asp:TableCell runat="server">

<asp:RadioButton GroupName="sex" Font-Size="12px" Text="男性" ID="sexMale" Runat="server"   Checked="true" />

<asp:RadioButton GroupName="sex" Font-Size="12px" Text="女性" ID="sexFeMale" Runat="server" />

</asp:TableCell>

</asp:TableRow>

<asp:TableRow runat="server">

<asp:TableCell HorizontalAlign="Right" runat="server"><asp:Label ID="Label5" Font-size="12px" Runat="server" Text="生日" Font-Names="verdana" /></asp:TableCell>

<asp:TableCell runat="server"><asp:TextBox id="txtPersonDob" runat="server" /></asp:TableCell>

</asp:TableRow>

<asp:TableRow runat="server">

<asp:TableCell HorizontalAlign="Right" runat="server"><asp:Label ID="Label6" Font-size="12px" Runat="server" Text="图片" Font-Names="verdana" /></asp:TableCell>

<asp:TableCell runat="server"><input type="file" id="PersonImage" runat="server" /></asp:TableCell>

</asp:TableRow>

<asp:TableRow runat="server">

<asp:TableCell ColumnSpan="2" HorizontalAlign="Center" runat="server">

<asp:Button ID="Button1" Text="添加" OnClick="AddPerson" runat="server" />

</asp:TableCell>

</asp:TableRow>

</asp:Table>
<br />
<table>
<tr>
<td align="left" valign="top" style="width: 181px">
<asp:Label ID="Label_Message" runat="server" Font-Size="10pt"></asp:Label></td>
<td style="width: 7px">
</td>
<td align="left" style="width: 18px" valign="top">
</td>
</tr>
<tr>
<td style="width: 181px;" align="left" valign="top">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="PersonID" DataSourceID="SqlDataSource_SelectImage" ForeColor="#333333" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Font-Size="10pt" Width="200px" BackColor="#FFE0C0">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="序号" InsertVisible="False" SortExpression="PersonID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("PersonID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("PersonID") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="35px" />
</asp:TemplateField>
<asp:BoundField DataField="PersonName" HeaderText="名字" SortExpression="PersonName" />
<asp:BoundField DataField="PersonImageType" HeaderText="图片类型" SortExpression="PersonImageType" />
<asp:CommandField ShowSelectButton="True" >
<ItemStyle Width="30px" />
</asp:CommandField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="Red" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
<td style="width: 7px; height: 180px;">
</td>
<td align="left" style="width: 18px; height: 180px;" valign="top">
<asp:Image ID="Image_Show" runat="server" /></td>
</tr>
<tr>
<td style="width: 181px">
</td>
<td style="width: 7px">
</td>
<td style="width: 18px">
</td>
</tr>
<tr>
<td style="width: 181px; height: 21px;">
</td>
<td style="width: 7px; height: 21px;">
</td>
<td style="width: 18px; height: 21px;">
</td>
</tr>
</table>
<asp:SqlDataSource ID="SqlDataSource_SelectImage" runat="server" ConnectionString="<%$ ConnectionStrings:ImageSelect_ConnectionString %>"
SelectCommand="SELECT PersonID, PersonName, PersonImageType FROM Person"></asp:SqlDataSource>

</form>
</body>
</html>

ImageFileUpload.aspx.cs:

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.IO;

public partial class ImageFileUpload : System.Web.UI.Page

ImageShow.aspx:(显示图片)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImageShow.aspx.cs" Inherits="ImageShow" %>

<!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>

ImageShow.aspx.cs:

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;
public partial class ImageShow : System.Web.UI.Page

最后一个存储过程:
Drop Table Person

Go

Create Table Person

(

PersonID Int Identity,

PersonEmail Varchar(255),

PersonName Varchar(255),

PersonSex Char(1),

PersonDOB DateTime,

PersonImage Image,

PersonImageType Varchar(255)

)

Drop Proc sp_person_isp

Go

Create Proc sp_person_isp

@PersonEmail Varchar(255),

@PersonName Varchar(255),

@PersonSex Char(1),

@PersonDOB DateTime,

@PersonImage Image,

@PersonImageType Varchar(255)

As

Begin

Insert into Person

(PersonEmail, PersonName, PersonSex,

PersonDOB, PersonImage, PersonImageType)

Values

(@PersonEmail, @PersonName, @PersonSex,

@PersonDOB, @PersonImage, @PersonImageType)

End

Go
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: