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

news/2025/2/23 15:31:55

最近捣鼓了一下,效果就是这样:
图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

 


http://www.niftyadmin.cn/n/1412704.html

相关文章

Java SE 枚举类

Java SE 枚举类 作者&#xff1a; [霍英俊] (huo920live.com) 文章目录Java SE 枚举类1、枚举类理解2、自定义枚举类&#xff08;jdk5.0前&#xff09;3、使用接口定义静态常量4、使用enum定义枚举类&#xff08;jdk5.0新增&#xff09;&#xff08;重点&#xff09;1 、说明介…

关闭ESXi https的欢迎页面,增强服务器的安全。

原因&#xff1a;在装好ESXi主机之后&#xff0c;使用 https://ip地址 是可以直接打开ESXi的Web欢迎页面的。但是&#xff0c;欢迎界面其实会带来很多不安全的因素。因此必要情况&#xff0c;关闭esxi欢迎界面能从某些程度增强服务器的安全性。隐藏esxi欢迎页原因&#xff1a;…

iOS - (几个 button 按钮之间的单选与多选)

先来看看效果图&#xff1a; 下面是实现的代码&#xff1a; 首先创建10个button&#xff08;一个一个写太麻烦了&#xff0c;个人认为还是用一个 for 循环来创建比较好&#xff09; 下面就是 button 的点击方法实现单选 多选的比较好做&#xff0c;写法也简单&#xff0c;只需做…

vscode spring boot配置文件application.properties不提示解决方式

背景 因实际的编程环境是jdk1.6&#xff0c;vscode安装了spring boot tools开发后&#xff0c;application.properties无法提示。spring boot tools的功能之一就是提示application.properties&#xff0c;所以问题就在于spring boot tools加载出了问题。 编程环境 系统环境变量…

Java 设计模式-工厂方法模式

Java 设计模式-工厂方法模式 文章目录Java 设计模式-工厂方法模式1. 介绍2. 模式原理3. 实现4. 优点5. 缺点6. 应用场景1. 介绍 1.1 定义 工厂方法模式&#xff08;FACTORY METHOD&#xff09;是一种常用的类创建型设计模式,此模式的核心精神是封装类中变化的部分&#xff0c…

一个右键菜单的JS类

右键菜单在web交互设计里并不是很常用的东西&#xff0c;因为普通用户在浏览网页时很少会想到能用右键来操作&#xff0c;就像他们很难习惯拖拽一样&#xff0c;但在某些环境中&#xff0c;拖拽会成为很自然的操作——其实都跟桌面软件的操作习惯有关。所以右键菜单在某些情况下…

冯诺依曼体系结构与哈佛体系结构的区别【转】

本文转载自:http://www.cnblogs.com/li-hao/archive/2011/12/21/2296010.html 材料一: 哈佛结构是一种将程序指令存储和数据存储分开的存储器结构。中央处理器首先到程序指令存储器中读取程序指令内容&#xff0c;解码后得到数据地址&#xff0c;再到相应的数据存储器中读取数据…

JAVA IO - 创建并写文件的五种方式

JAVA IO - 创建并写文件的五种方式 Test void testCreateFile1() throws IOException {String fileName "D:\data\test\newFile.txt";Path path Paths.get(fileName);// 使用newBufferedWriter创建文件并写文件// 这里使用了try-with-resources方法来关闭流&#…