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

本文介绍了一个基于 ASP.NET 的图片上传与显示系统实现,包括前端页面设计、后端处理逻辑及数据库交互过程。该系统能够接收用户上传的图片,并将其存储到数据库中,同时展示已上传的图片。

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

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


图2:


ImageFileUpload.aspx:

None.gif<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImageFileUpload.aspx.cs" Inherits="ImageFileUpload" %>
None.gif
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
None.gif
<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif
<head runat="server">
None.gif    
<title>图片保存和显示</title>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="Form2" enctype="multipart/form-data" runat="server">
None.gif
None.gif            
<asp:Table ID="Table1" Runat="server" Width="39%" BorderWidth="1" BackColor="Beige">
None.gif
None.gif                        
<asp:TableRow runat="server">
None.gif
None.gif                                    
<asp:TableCell ColumnSpan="2" BackColor="Red" runat="server">
None.gif
None.gif                                    
<asp:Label ID="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:TableRow runat="server">
None.gif
None.gif                                    
<asp:TableCell HorizontalAlign="Right" runat="server"><asp:Label ID="Label2" Font-size="12px"  Runat="server" Text="名字" Font-Names="verdana" /></asp:TableCell>
None.gif
None.gif                                    
<asp:TableCell runat="server"><asp:TextBox id="txtPersonName" Runat="server" /></asp:TableCell>
None.gif
None.gif                        
</asp:TableRow>
None.gif
None.gif                        
<asp:TableRow runat="server">
None.gif
None.gif                                    
<asp:TableCell HorizontalAlign="Right" runat="server"><asp:Label ID="Label3" Font-size="12px"  Runat="server" Text="Email" Font-Names="verdana" /></asp:TableCell>
None.gif
None.gif                                    
<asp:TableCell runat="server"><asp:TextBox id="txtPersonEmail" Runat="server" /></asp:TableCell>
None.gif
None.gif                        
</asp:TableRow>
None.gif
None.gif                        
<asp:TableRow runat="server">
None.gif
None.gif                                    
<asp:TableCell HorizontalAlign="Right" runat="server"><asp:Label ID="Label4" Font-size="12px"  Runat="server" Text="性别" Font-Names="verdana" /></asp:TableCell>
None.gif
None.gif                                    
<asp:TableCell runat="server">
None.gif
None.gif                                                
<asp:RadioButton GroupName="sex"  Font-Size="12px" Text="男性" ID="sexMale" Runat="server"   Checked="true" />
None.gif
None.gif                                                
<asp:RadioButton GroupName="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:TableRow runat="server">
None.gif
None.gif                                    
<asp:TableCell HorizontalAlign="Right" runat="server"><asp:Label ID="Label5" Font-size="12px"  Runat="server" Text="生日" Font-Names="verdana" /></asp:TableCell>
None.gif
None.gif                                    
<asp:TableCell runat="server"><asp:TextBox id="txtPersonDob" runat="server" /></asp:TableCell>
None.gif
None.gif                        
</asp:TableRow>
None.gif
None.gif                        
<asp:TableRow runat="server">
None.gif
None.gif                                    
<asp:TableCell HorizontalAlign="Right" runat="server"><asp:Label ID="Label6" Font-size="12px"  Runat="server" Text="图片" Font-Names="verdana" /></asp:TableCell>
None.gif
None.gif                                    
<asp:TableCell runat="server"><input type="file" id="PersonImage" runat="server" /></asp:TableCell>
None.gif
None.gif                        
</asp:TableRow>
None.gif
None.gif                        
<asp:TableRow runat="server">
None.gif
None.gif                                    
<asp:TableCell ColumnSpan="2" HorizontalAlign="Center" runat="server">
None.gif
None.gif                                    
<asp:Button ID="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                
<td align="left" valign="top" style="width: 181px">
None.gif                    
<asp:Label ID="Label_Message" runat="server" Font-Size="10pt"></asp:Label></td>
None.gif                
<td style="width: 7px">
None.gif                
</td>
None.gif                
<td align="left" style="width: 18px" valign="top">
None.gif                
</td>
None.gif            
</tr>
None.gif            
<tr>
None.gif                
<td style="width: 181px;" align="left" valign="top">
None.gif                    
<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">
None.gif                        
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
None.gif                        
<Columns>
None.gif                            
<asp:TemplateField HeaderText="序号" InsertVisible="False" SortExpression="PersonID">
None.gif                                
<EditItemTemplate>
None.gif                                    
<asp:Label ID="Label1" runat="server" Text='<%# Eval("PersonID") %>'></asp:Label>
None.gif                                
</EditItemTemplate>
None.gif                                
<ItemTemplate>
None.gif                                    
<asp:Label ID="Label1" runat="server" Text='<%# Bind("PersonID") %>'></asp:Label>
None.gif                                
</ItemTemplate>
None.gif                                
<HeaderStyle Width="35px" />
None.gif                            
</asp:TemplateField>
None.gif                            
<asp:BoundField DataField="PersonName" HeaderText="名字" SortExpression="PersonName" />
None.gif                            
<asp:BoundField DataField="PersonImageType" HeaderText="图片类型" SortExpression="PersonImageType" />
None.gif                            
<asp:CommandField ShowSelectButton="True" >
None.gif                                
<ItemStyle Width="30px" />
None.gif                            
</asp:CommandField>
None.gif                        
</Columns>
None.gif                        
<RowStyle BackColor="#EFF3FB" />
None.gif                        
<EditRowStyle BackColor="#2461BF" />
None.gif                        
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
None.gif                        
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
None.gif                        
<HeaderStyle BackColor="Red" Font-Bold="True" ForeColor="White" />
None.gif                        
<AlternatingRowStyle BackColor="White" />
None.gif                    
</asp:GridView>
None.gif                
</td>
None.gif                
<td style="width: 7px; height: 180px;">
None.gif                
</td>
None.gif                
<td align="left" style="width: 18px; height: 180px;" valign="top">
None.gif                    
<asp:Image ID="Image_Show" runat="server" /></td>
None.gif            
</tr>
None.gif            
<tr>
None.gif                
<td style="width: 181px">
None.gif                    
</td>
None.gif                
<td style="width: 7px">
None.gif                
</td>
None.gif                
<td style="width: 18px">
None.gif                
</td>
None.gif            
</tr>
None.gif            
<tr>
None.gif                
<td style="width: 181px; height: 21px;">
None.gif                
</td>
None.gif                
<td style="width: 7px; height: 21px;">
None.gif                
</td>
None.gif                
<td style="width: 18px; height: 21px;">
None.gif                
</td>
None.gif            
</tr>
None.gif        
</table>
None.gif        
<asp:SqlDataSource ID="SqlDataSource_SelectImage" runat="server" ConnectionString="<%$ ConnectionStrings:ImageSelect_ConnectionString %>"
None.gif            SelectCommand
="SELECT PersonID, PersonName, PersonImageType FROM Person"></asp:SqlDataSource>
None.gif
None.gif    
</form>
None.gif
</body>
None.gif
</html>
None.gif

ImageFileUpload.aspx.cs:
None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Collections;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Data.SqlClient;
None.gif
using System.IO;
None.gif
None.gif
None.gif
None.gif
None.gif
public partial class ImageFileUpload : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif    
protected void AddPerson(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int intImageSize;
InBlock.gif        
string strImageType;
InBlock.gif        Stream ImageStream;
InBlock.gif
InBlock.gif         intImageSize 
= PersonImage.PostedFile.ContentLength;
InBlock.gif         ImageStream 
= PersonImage.PostedFile.InputStream;
InBlock.gif         strImageType 
= PersonImage.PostedFile.ContentType;
InBlock.gif        Byte[] ImageContent 
= new Byte[intImageSize];
InBlock.gif        
int  intStatus;
InBlock.gif        intStatus 
= ImageStream.Read(ImageContent, 0, intImageSize);
InBlock.gif
InBlock.gif        
//string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["Lw_SqlConnectString"];
InBlock.gif
        string ConnStr = "Data Source=127.0.0.1;Initial Catalog=Pfrmis_TZXY;User ID=sa";
InBlock.gif        SqlConnection myConnection
=new SqlConnection(ConnStr);
InBlock.gif        SqlCommand  myCommand
=new SqlCommand("sp_person_isp", myConnection);
InBlock.gif        myCommand.CommandType 
= CommandType.StoredProcedure;
InBlock.gif
InBlock.gif        SqlParameter prmEmail
=new SqlParameter("@PersonEmail", SqlDbType.VarChar, 255);
InBlock.gif
InBlock.gif        prmEmail.Value 
= txtPersonEmail.Text;
InBlock.gif        myCommand.Parameters.Add(prmEmail);
InBlock.gif
InBlock.gif        SqlParameter prmName
=new SqlParameter("@PersonName", SqlDbType.VarChar, 255);
InBlock.gif
InBlock.gif        prmName.Value 
= txtPersonName.Text;
InBlock.gif        myCommand.Parameters.Add(prmName);
InBlock.gif
InBlock.gif        SqlParameter prmSex 
=new SqlParameter("@PersonSex", SqlDbType.Char, 1);
InBlock.gif        
if (sexMale.Checked) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            prmSex.Value 
= "M";
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        myCommand.Parameters.Add(prmSex);
InBlock.gif
InBlock.gif        SqlParameter prmPersonDOB
= new SqlParameter("@PersonDOB", SqlDbType.DateTime);
InBlock.gif
InBlock.gif        prmPersonDOB.Value 
= txtPersonDob.Text;
InBlock.gif
InBlock.gif        myCommand.Parameters.Add(prmPersonDOB);
InBlock.gif
InBlock.gif        SqlParameter prmPersonImage 
=new SqlParameter("@PersonImage", SqlDbType.Image);
InBlock.gif
InBlock.gif        prmPersonImage.Value 
= ImageContent;
InBlock.gif
InBlock.gif        myCommand.Parameters.Add(prmPersonImage);
InBlock.gif
InBlock.gif        SqlParameter prmPersonImageType 
=new SqlParameter("@PersonImageType", SqlDbType.VarChar, 255);
InBlock.gif
InBlock.gif        prmPersonImageType.Value 
= strImageType;
InBlock.gif
InBlock.gif        myCommand.Parameters.Add(prmPersonImageType);
InBlock.gif
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            myConnection.Open();
InBlock.gif            myCommand.ExecuteNonQuery();
InBlock.gif
InBlock.gif            myConnection.Close();
InBlock.gif
InBlock.gif            Response.Write(
"添加成功!");
InBlock.gif
ExpandedSubBlockEnd.gif        }

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

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

InBlock.gif
InBlock.gif 
InBlock.gif    
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif       
// Session["id"] = "";
InBlock.gif
       
InBlock.gif        Label lbl1;
InBlock.gif        lbl1 
= (Label)GridView1.SelectedRow.Cells[0].FindControl("Label1");
InBlock.gif
InBlock.gif        Label_Message.Text 
="你选择的图片是:"+lbl1.Text;
InBlock.gif        Session[
"id"= lbl1.Text;
InBlock.gif        Image_Show.ImageUrl 
= "ImageShow.aspx";
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

ImageShow.aspx:(显示图片)
None.gif<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImageShow.aspx.cs" Inherits="ImageShow" %>
None.gif
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
None.gif
<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif
<head runat="server">
None.gif    
<title>无标题页</title>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="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.gifusing System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Collections;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Data.SqlClient;
None.gif
public partial class ImageShow : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//连接数据库的第一种方法
InBlock.gif
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["Lw_SqlConnectString"]);
InBlock.gif
InBlock.gif        
//连接数据库的第二种方法
InBlock.gif        
//SqlConnection myConnection = new SqlConnection("Data Source=.;Initial Catalog=Tcis_shrl;User Id=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            
string sql = "Select PersonImage from Person Where PersonID=";
InBlock.gif            SqlCommand myCommand 
= new SqlCommand(sql + Session["id"].ToString().Trim(), myConnection);
InBlock.gif            
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myConnection.Open();
InBlock.gif                SqlDataReader myDataReader;
InBlock.gif                myDataReader 
= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
InBlock.gif                
if (myDataReader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Response.Clear();
InBlock.gif
InBlock.gif                   
InBlock.gif                  
//  Response.ContentType = "image/pjpeg";//可有可无
InBlock.gif
                    Response.BinaryWrite((byte[])myDataReader["PersonImage"]);
ExpandedSubBlockEnd.gif                }

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

InBlock.gif            
catch (SqlException SQLexc)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                 Response.Write(SQLexc);
ExpandedSubBlockEnd.gif            }

InBlock.gif            Response.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

 

转载于:https://www.cnblogs.com/cry/archive/2007/01/18/623973.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值