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

本文介绍了一个使用ASP.NET实现的图片上传及显示功能的具体实现方法。通过C#代码完成图片信息的收集、存储和展示,并详细展示了如何利用ASP.NET中的控件进行布局设计和交互操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近捣鼓了一下,效果就是这样:
图1:


图2:


ImageFileUpload.aspx:

None.gif<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="ImageFileUpload.aspx.cs"Inherits="ImageFileUpload"%>
None.gif
None.gif
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
None.gif
<htmlxmlns="http://www.w3.org/1999/xhtml">
None.gif
<headrunat="server">
None.gif
<title>图片保存和显示</title>
None.gif
</head>
None.gif
<body>
None.gif
<formid="Form2"enctype="multipart/form-data"runat="server">
None.gif
None.gif
<asp:TableID="Table1"Runat="server"Width="39%"BorderWidth="1"BackColor="Beige">
None.gif
None.gif
<asp:TableRowrunat="server">
None.gif
None.gif
<asp:TableCellColumnSpan="2"BackColor="Red"runat="server">
None.gif
None.gif
<asp:LabelID="Label1"Font-size="12px"ForeColor="White"font-bold="True"Runat="server"Text="添加"Font-Names="verdana"/>
None.gif
None.gif
</asp:TableCell>
None.gif
None.gif
</asp:TableRow>
None.gif
None.gif
<asp:TableRowrunat="server">
None.gif
None.gif
<asp:TableCellHorizontalAlign="Right"runat="server"><asp:LabelID="Label2"Font-size="12px"Runat="server"Text="名字"Font-Names="verdana"/></asp:TableCell>
None.gif
None.gif
<asp:TableCellrunat="server"><asp:TextBoxid="txtPersonName"Runat="server"/></asp:TableCell>
None.gif
None.gif
</asp:TableRow>
None.gif
None.gif
<asp:TableRowrunat="server">
None.gif
None.gif
<asp:TableCellHorizontalAlign="Right"runat="server"><asp:LabelID="Label3"Font-size="12px"Runat="server"Text="Email"Font-Names="verdana"/></asp:TableCell>
None.gif
None.gif
<asp:TableCellrunat="server"><asp:TextBoxid="txtPersonEmail"Runat="server"/></asp:TableCell>
None.gif
None.gif
</asp:TableRow>
None.gif
None.gif
<asp:TableRowrunat="server">
None.gif
None.gif
<asp:TableCellHorizontalAlign="Right"runat="server"><asp:LabelID="Label4"Font-size="12px"Runat="server"Text="性别"Font-Names="verdana"/></asp:TableCell>
None.gif
None.gif
<asp:TableCellrunat="server">
None.gif
None.gif
<asp:RadioButtonGroupName="sex"Font-Size="12px"Text="男性"ID="sexMale"Runat="server" Checked="true"/>
None.gif
None.gif
<asp:RadioButtonGroupName="sex"Font-Size="12px"Text="女性"ID="sexFeMale"Runat="server"/>
None.gif
None.gif
</asp:TableCell>
None.gif
None.gif
</asp:TableRow>
None.gif
None.gif
<asp:TableRowrunat="server">
None.gif
None.gif
<asp:TableCellHorizontalAlign="Right"runat="server"><asp:LabelID="Label5"Font-size="12px"Runat="server"Text="生日"Font-Names="verdana"/></asp:TableCell>
None.gif
None.gif
<asp:TableCellrunat="server"><asp:TextBoxid="txtPersonDob"runat="server"/></asp:TableCell>
None.gif
None.gif
</asp:TableRow>
None.gif
None.gif
<asp:TableRowrunat="server">
None.gif
None.gif
<asp:TableCellHorizontalAlign="Right"runat="server"><asp:LabelID="Label6"Font-size="12px"Runat="server"Text="图片"Font-Names="verdana"/></asp:TableCell>
None.gif
None.gif
<asp:TableCellrunat="server"><inputtype="file"id="PersonImage"runat="server"/></asp:TableCell>
None.gif
None.gif
</asp:TableRow>
None.gif
None.gif
<asp:TableRowrunat="server">
None.gif
None.gif
<asp:TableCellColumnSpan="2"HorizontalAlign="Center"runat="server">
None.gif
None.gif
<asp:ButtonID="Button1"Text="添加"OnClick="AddPerson"runat="server"/>
None.gif
None.gif
</asp:TableCell>
None.gif
None.gif
</asp:TableRow>
None.gif
None.gif
</asp:Table>
None.gif
<br/>
None.gif
<table>
None.gif
<tr>
None.gif
<tdalign="left"valign="top"style="width:181px">
None.gif
<asp:LabelID="Label_Message"runat="server"Font-Size="10pt"></asp:Label></td>
None.gif
<tdstyle="width:7px">
None.gif
</td>
None.gif
<tdalign="left"style="width:18px"valign="top">
None.gif
</td>
None.gif
</tr>
None.gif
<tr>
None.gif
<tdstyle="width:181px;"align="left"valign="top">
None.gif
<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"CellPadding="4"DataKeyNames="PersonID"DataSourceID="SqlDataSource_SelectImage"ForeColor="#333333"OnSelectedIndexChanged="GridView1_SelectedIndexChanged"Font-Size="10pt"Width="200px"BackColor="#FFE0C0">
None.gif
<FooterStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/>
None.gif
<Columns>
None.gif
<asp:TemplateFieldHeaderText="序号"InsertVisible="False"SortExpression="PersonID">
None.gif
<EditItemTemplate>
None.gif
<asp:LabelID="Label1"runat="server"Text='<%#Eval("PersonID")%>'></asp:Label>
None.gif
</EditItemTemplate>
None.gif
<ItemTemplate>
None.gif
<asp:LabelID="Label1"runat="server"Text='<%#Bind("PersonID")%>'></asp:Label>
None.gif
</ItemTemplate>
None.gif
<HeaderStyleWidth="35px"/>
None.gif
</asp:TemplateField>
None.gif
<asp:BoundFieldDataField="PersonName"HeaderText="名字"SortExpression="PersonName"/>
None.gif
<asp:BoundFieldDataField="PersonImageType"HeaderText="图片类型"SortExpression="PersonImageType"/>
None.gif
<asp:CommandFieldShowSelectButton="True">
None.gif
<ItemStyleWidth="30px"/>
None.gif
</asp:CommandField>
None.gif
</Columns>
None.gif
<RowStyleBackColor="#EFF3FB"/>
None.gif
<EditRowStyleBackColor="#2461BF"/>
None.gif
<SelectedRowStyleBackColor="#D1DDF1"Font-Bold="True"ForeColor="#333333"/>
None.gif
<PagerStyleBackColor="#2461BF"ForeColor="White"HorizontalAlign="Center"/>
None.gif
<HeaderStyleBackColor="Red"Font-Bold="True"ForeColor="White"/>
None.gif
<AlternatingRowStyleBackColor="White"/>
None.gif
</asp:GridView>
None.gif
</td>
None.gif
<tdstyle="width:7px;height:180px;">
None.gif
</td>
None.gif
<tdalign="left"style="width:18px;height:180px;"valign="top">
None.gif
<asp:ImageID="Image_Show"runat="server"/></td>
None.gif
</tr>
None.gif
<tr>
None.gif
<tdstyle="width:181px">
None.gif
</td>
None.gif
<tdstyle="width:7px">
None.gif
</td>
None.gif
<tdstyle="width:18px">
None.gif
</td>
None.gif
</tr>
None.gif
<tr>
None.gif
<tdstyle="width:181px;height:21px;">
None.gif
</td>
None.gif
<tdstyle="width:7px;height:21px;">
None.gif
</td>
None.gif
<tdstyle="width:18px;height:21px;">
None.gif
</td>
None.gif
</tr>
None.gif
</table>
None.gif
<asp:SqlDataSourceID="SqlDataSource_SelectImage"runat="server"ConnectionString="<%$ConnectionStrings:ImageSelect_ConnectionString%>"
None.gifSelectCommand
="SELECTPersonID,PersonName,PersonImageTypeFROMPerson"></asp:SqlDataSource>
None.gif
None.gif
</form>
None.gif
</body>
None.gif
</html>
None.gif

ImageFileUpload.aspx.cs:
None.gifusingSystem;
None.gif
usingSystem.Data;
None.gif
usingSystem.Configuration;
None.gif
usingSystem.Collections;
None.gif
usingSystem.Web;
None.gif
usingSystem.Web.Security;
None.gif
usingSystem.Web.UI;
None.gif
usingSystem.Web.UI.WebControls;
None.gif
usingSystem.Web.UI.WebControls.WebParts;
None.gif
usingSystem.Web.UI.HtmlControls;
None.gif
usingSystem.Data.SqlClient;
None.gif
usingSystem.IO;
None.gif
None.gif
None.gif
None.gif
None.gif
publicpartialclassImageFileUpload:System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
protectedvoidPage_Load(objectsender,EventArgse)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
protectedvoidAddPerson(objectsender,EventArgse)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
intintImageSize;
InBlock.gif
stringstrImageType;
InBlock.gifStreamImageStream;
InBlock.gif
InBlock.gifintImageSize
=PersonImage.PostedFile.ContentLength;
InBlock.gifImageStream
=PersonImage.PostedFile.InputStream;
InBlock.gifstrImageType
=PersonImage.PostedFile.ContentType;
InBlock.gifByte[]ImageContent
=newByte[intImageSize];
InBlock.gif
intintStatus;
InBlock.gifintStatus
=ImageStream.Read(ImageContent,0,intImageSize);
InBlock.gif
InBlock.gif
//stringConnStr=System.Configuration.ConfigurationSettings.AppSettings["Lw_SqlConnectString"];
InBlock.gif
stringConnStr="DataSource=127.0.0.1;InitialCatalog=Pfrmis_TZXY;UserID=sa";
InBlock.gifSqlConnectionmyConnection
=newSqlConnection(ConnStr);
InBlock.gifSqlCommandmyCommand
=newSqlCommand("sp_person_isp",myConnection);
InBlock.gifmyCommand.CommandType
=CommandType.StoredProcedure;
InBlock.gif
InBlock.gifSqlParameterprmEmail
=newSqlParameter("@PersonEmail",SqlDbType.VarChar,255);
InBlock.gif
InBlock.gifprmEmail.Value
=txtPersonEmail.Text;
InBlock.gifmyCommand.Parameters.Add(prmEmail);
InBlock.gif
InBlock.gifSqlParameterprmName
=newSqlParameter("@PersonName",SqlDbType.VarChar,255);
InBlock.gif
InBlock.gifprmName.Value
=txtPersonName.Text;
InBlock.gifmyCommand.Parameters.Add(prmName);
InBlock.gif
InBlock.gifSqlParameterprmSex
=newSqlParameter("@PersonSex",SqlDbType.Char,1);
InBlock.gif
if(sexMale.Checked)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifprmSex.Value
="M";
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifprmSex.Value
="F";
ExpandedSubBlockEnd.gif}

InBlock.gifmyCommand.Parameters.Add(prmSex);
InBlock.gif
InBlock.gifSqlParameterprmPersonDOB
=newSqlParameter("@PersonDOB",SqlDbType.DateTime);
InBlock.gif
InBlock.gifprmPersonDOB.Value
=txtPersonDob.Text;
InBlock.gif
InBlock.gifmyCommand.Parameters.Add(prmPersonDOB);
InBlock.gif
InBlock.gifSqlParameterprmPersonImage
=newSqlParameter("@PersonImage",SqlDbType.Image);
InBlock.gif
InBlock.gifprmPersonImage.Value
=ImageContent;
InBlock.gif
InBlock.gifmyCommand.Parameters.Add(prmPersonImage);
InBlock.gif
InBlock.gifSqlParameterprmPersonImageType
=newSqlParameter("@PersonImageType",SqlDbType.VarChar,255);
InBlock.gif
InBlock.gifprmPersonImageType.Value
=strImageType;
InBlock.gif
InBlock.gifmyCommand.Parameters.Add(prmPersonImageType);
InBlock.gif
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gifmyConnection.Open();
InBlock.gifmyCommand.ExecuteNonQuery();
InBlock.gif
InBlock.gifmyConnection.Close();
InBlock.gif
InBlock.gifResponse.Write(
"添加成功!");
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(SqlExceptionSQLexc)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifResponse.Write(
"插入失败,错误如下:"+SQLexc.ToString());
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif
protectedvoidGridView1_SelectedIndexChanged(objectsender,EventArgse)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
//Session["id"]="";
InBlock.gif

InBlock.gifLabellbl1;
InBlock.giflbl1
=(Label)GridView1.SelectedRow.Cells[0].FindControl("Label1");
InBlock.gif
InBlock.gifLabel_Message.Text
="你选择的图片是:"+lbl1.Text;
InBlock.gifSession[
"id"]=lbl1.Text;
InBlock.gifImage_Show.ImageUrl
="ImageShow.aspx";
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

ImageShow.aspx:(显示图片)
None.gif<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="ImageShow.aspx.cs"Inherits="ImageShow"%>
None.gif
None.gif
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
None.gif
<htmlxmlns="http://www.w3.org/1999/xhtml">
None.gif
<headrunat="server">
None.gif
<title>无标题页</title>
None.gif
</head>
None.gif
<body>
None.gif
<formid="form1"runat="server">
None.gif
<div>
None.gif
None.gif
</div>
None.gif
</form>
None.gif
</body>
None.gif
</html>
None.gif

ImageShow.aspx.cs:

None.gifusingSystem;
None.gif
usingSystem.Data;
None.gif
usingSystem.Configuration;
None.gif
usingSystem.Collections;
None.gif
usingSystem.Web;
None.gif
usingSystem.Web.Security;
None.gif
usingSystem.Web.UI;
None.gif
usingSystem.Web.UI.WebControls;
None.gif
usingSystem.Web.UI.WebControls.WebParts;
None.gif
usingSystem.Web.UI.HtmlControls;
None.gif
usingSystem.Data.SqlClient;
None.gif
publicpartialclassImageShow:System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
protectedvoidPage_Load(objectsender,EventArgse)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//连接数据库的第一种方法
InBlock.gif
SqlConnectionmyConnection=newSqlConnection(ConfigurationManager.AppSettings["Lw_SqlConnectString"]);
InBlock.gif
InBlock.gif
//连接数据库的第二种方法
InBlock.gif
//SqlConnectionmyConnection=newSqlConnection("DataSource=.;InitialCatalog=Tcis_shrl;UserId=sa;Password=;");
InBlock.gif
if(Session["id"]==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
stringsql="SelectPersonImagefromPersonWherePersonID=";
InBlock.gifSqlCommandmyCommand
=newSqlCommand(sql+Session["id"].ToString().Trim(),myConnection);
InBlock.gif
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifmyConnection.Open();
InBlock.gifSqlDataReadermyDataReader;
InBlock.gifmyDataReader
=myCommand.ExecuteReader(CommandBehavior.CloseConnection);
InBlock.gif
if(myDataReader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifResponse.Clear();
InBlock.gif
InBlock.gif
InBlock.gif
//Response.ContentType="image/pjpeg";//可有可无
InBlock.gif
Response.BinaryWrite((byte[])myDataReader["PersonImage"]);
ExpandedSubBlockEnd.gif}

InBlock.gifmyConnection.Close();
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(SqlExceptionSQLexc)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifResponse.Write(SQLexc);
ExpandedSubBlockEnd.gif}

InBlock.gifResponse.End();
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif


最后一个存储过程:
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值