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

50 篇文章 2 订阅
28 篇文章 0 订阅

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


图2:


ImageFileUpload.aspx:

<% @ 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
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }






    
protected void AddPerson(object sender, EventArgs e)
    
{
        
int intImageSize;
        
string strImageType;
        Stream ImageStream;

         intImageSize 
= PersonImage.PostedFile.ContentLength;
         ImageStream 
= PersonImage.PostedFile.InputStream;
         strImageType 
= PersonImage.PostedFile.ContentType;
        Byte[] ImageContent 
= new Byte[intImageSize];
        
int  intStatus;
        intStatus 
= ImageStream.Read(ImageContent, 0, intImageSize);

        
//string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["Lw_SqlConnectString"];
        string ConnStr = "Data Source=127.0.0.1;Initial Catalog=Pfrmis_TZXY;User ID=sa";
        SqlConnection myConnection
=new SqlConnection(ConnStr);
        SqlCommand  myCommand
=new SqlCommand("sp_person_isp", myConnection);
        myCommand.CommandType 
= CommandType.StoredProcedure;

        SqlParameter prmEmail
=new SqlParameter("@PersonEmail", SqlDbType.VarChar, 255);

        prmEmail.Value 
= txtPersonEmail.Text;
        myCommand.Parameters.Add(prmEmail);

        SqlParameter prmName
=new SqlParameter("@PersonName", SqlDbType.VarChar, 255);

        prmName.Value 
= txtPersonName.Text;
        myCommand.Parameters.Add(prmName);

        SqlParameter prmSex 
=new SqlParameter("@PersonSex", SqlDbType.Char, 1);
        
if (sexMale.Checked) 
        
{
            prmSex.Value 
= "M";
        }

        
else
        
{
             prmSex.Value 
= "F";
        }

        myCommand.Parameters.Add(prmSex);

        SqlParameter prmPersonDOB
= new SqlParameter("@PersonDOB", SqlDbType.DateTime);

        prmPersonDOB.Value 
= txtPersonDob.Text;

        myCommand.Parameters.Add(prmPersonDOB);

        SqlParameter prmPersonImage 
=new SqlParameter("@PersonImage", SqlDbType.Image);

        prmPersonImage.Value 
= ImageContent;

        myCommand.Parameters.Add(prmPersonImage);

        SqlParameter prmPersonImageType 
=new SqlParameter("@PersonImageType", SqlDbType.VarChar, 255);

        prmPersonImageType.Value 
= strImageType;

        myCommand.Parameters.Add(prmPersonImageType);

        
try
        
{
            
            myConnection.Open();
            myCommand.ExecuteNonQuery();

            myConnection.Close();

            Response.Write(
"添加成功!");

        }

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

                                   


    }


 
    
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    
{

       
// Session["id"] = "";
       
        Label lbl1;
        lbl1 
= (Label)GridView1.SelectedRow.Cells[0].FindControl("Label1");

        Label_Message.Text 
="你选择的图片是:"+lbl1.Text;
        Session[
"id"= lbl1.Text;
        Image_Show.ImageUrl 
= "ImageShow.aspx";
    }

}


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
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
//连接数据库的第一种方法
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["Lw_SqlConnectString"]);

        
//连接数据库的第二种方法
        
//SqlConnection myConnection = new SqlConnection("Data Source=.;Initial Catalog=Tcis_shrl;User Id=sa;Password=;");
        if (Session["id"== null)
        
{

        }

        
else
        
{
            
string sql = "Select PersonImage from Person Where PersonID=";
            SqlCommand myCommand 
= new SqlCommand(sql + Session["id"].ToString().Trim(), myConnection);
            
            
try
            
{
                myConnection.Open();
                SqlDataReader myDataReader;
                myDataReader 
= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                
if (myDataReader.Read())
                
{
                    Response.Clear();

                   
                  
//  Response.ContentType = "image/pjpeg";//可有可无
                    Response.BinaryWrite((byte[])myDataReader["PersonImage"]);
                }

                myConnection.Close();
            }

            
catch (SqlException SQLexc)
            
{
                 Response.Write(SQLexc);
            }

            Response.End();
        }

    }

}


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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值