用C#实现图片数据库存储与显示

1.图片文件的存储,代码如下:(processImage.aspx)
<% @ Page language="c#" Codebehind="imageProcess.aspx.cs" AutoEventWireup="false" Inherits="imageProcess.imageProcess"  %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
< HTML >
    
< HEAD >
        
< title > imageProcess </ title >
        
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio .NET 7.1" >
        
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
        
< meta  name ="vs_defaultClientScript"  content ="JavaScript" >
        
< meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
    
</ HEAD >
    
< body  MS_POSITIONING ="GridLayout" >
        
< form  id ="Form1"  method ="post"  runat ="server" >
            
< FONT  face ="宋体" >< INPUT  style ="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 32px"  type ="file"  name ="File1"
                    id
="File1"  runat ="server" >
                
< asp:TextBox  id ="txtDescript"  style ="Z-INDEX: 102; LEFT: 48px; POSITION: absolute; TOP: 72px"
                    runat
="server"  TextMode ="MultiLine"  Width ="224px"  Height ="64px" ></ asp:TextBox >
                
< asp:Button  id ="btnUpload"  style ="Z-INDEX: 103; LEFT: 216px; POSITION: absolute; TOP: 152px"
                    runat
="server"  Text ="上传"  Width ="56px" ></ asp:Button ></ FONT >
            
< asp:DataGrid  ID ="DG_Persons"  AutoGenerateColumns ="False"  Width ="632px"  HeaderStyle-BackColor ="#ff0000"
                HeaderStyle-Font-Bold
="True"  HeaderStyle-ForeColor ="#ffffff"  ItemStyle-BackColor ="Beige"  BorderColor ="Black"
                Runat
="server"  HeaderStyle-HorizontalAlign ="Center"  style ="Z-INDEX: 105; LEFT: 48px; POSITION: absolute; TOP: 208px" >
                
< ItemStyle  BackColor ="Beige" ></ ItemStyle >
                
< HeaderStyle  Font-Bold ="True"  HorizontalAlign ="Center"  ForeColor ="White"  BackColor ="Red" ></ HeaderStyle >
                
< Columns >
                    
< asp:TemplateColumn  HeaderText ="图片编号" >
                        
< ItemTemplate >
                            
< asp:Label  id =Label1  Text ='<%#  DataBinder.Eval(Container.DataItem, "ImgID") % > ' Runat="server">
                            
</ asp:Label >
                        
</ ItemTemplate >
                    
</ asp:TemplateColumn >
                    
< asp:TemplateColumn  HeaderText ="图片类型" >
                        
< ItemTemplate >
                            
< asp:Label  Runat ="server"  Text ='<%#  DataBinder.Eval(Container.DataItem, "ImageContentType") % > ' ID="Label2"/>
                        
</ ItemTemplate >
                    
</ asp:TemplateColumn >
                    
< asp:TemplateColumn  HeaderText ="图片大小" >
                        
< ItemTemplate >
                            
< asp:Label  Runat ="server"  Text ='<%#  DataBinder.Eval(Container.DataItem, "ImageSize") % > ' ID="Label3"/>
                        
</ ItemTemplate >
                    
</ asp:TemplateColumn >
                    
< asp:TemplateColumn  HeaderText ="照片" >
                        
< ItemTemplate >
                            
< asp:Image  Runat =server  ID ="Image2"  ImageUrl ='<%#  FormatURL(DataBinder.Eval(Container.DataItem, "ImgID")) % > ' />
                        
</ ItemTemplate >
                    
</ asp:TemplateColumn >
                
</ Columns >
            
</ asp:DataGrid >
        
</ form >
    
</ body >
</ HTML >
2.相应的后台代码processImage.aspx.cs代码如下:
using  System;
using  System.Collections;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Web;
using  System.Web.SessionState;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.HtmlControls;
using  System.Data.SqlClient;
using  System.IO;

namespace  imageProcess
{
    
/// <summary>
    
/// imageProcess 的摘要说明。
    
/// </summary>

    public class imageProcess : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.TextBox txtDescript;
        
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
        
protected System.Web.UI.WebControls.DataGrid DG_Persons;
        
protected System.Web.UI.WebControls.Button btnUpload;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            dataGridBind();
            
        }

        
private void dataGridBind()
        
{
            SqlConnection con
=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
            con.Open();
            SqlCommand cmd
=new SqlCommand("select ImgID,ImageContentType,ImageSize,ImageData from ImageStore",con);
            
this.DG_Persons.DataSource=cmd.ExecuteReader();
            
this.DG_Persons.DataBind();
            con.Close();
        }

        
Web 窗体设计器生成的代码

        
private void btnUpload_Click(object sender, System.EventArgs e)
        
{
            SqlConnection con
=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
            con.Open();
            FileStream fs
=File.Open(this.File1.PostedFile.FileName,FileMode.Open);
            
byte[] by=new byte[fs.Length];
            fs.Read(by,
0,(int)fs.Length);
            String SqlCmd 
= "insert into ImageStore(ImageData,ImageContentType,ImageDescription,ImageSize) values(@Image,@fileType,@fileDec,@fileSize)";
            SqlCommand CmdObj 
= new SqlCommand(SqlCmd, con);
            CmdObj.Parameters.Add(
"@Image",SqlDbType.Image, (int)fs.Length).Value = by;
            CmdObj.Parameters.Add(
"@fileType",SqlDbType.VarChar,20).Value=this.File1.PostedFile.ContentType;
            CmdObj.Parameters.Add(
"@fileDec",SqlDbType.VarChar,200).Value=this.txtDescript.Text;
            CmdObj.Parameters.Add(
"@fileSize",SqlDbType.BigInt).Value=this.File1.PostedFile.ContentLength;
            fs.Close();
            CmdObj.ExecuteNonQuery();
            dataGridBind();
        }

        
protected string FormatURL(object strArgument)
        
{
            
return "ReadImage.aspx?id=" + strArgument.ToString();
        }


    }

}

3.从数据库中读取图片
   新建一个readImage.aspx窗体文件,输入C#代码(readImage.aspx.cs)如下:
using  System;
using  System.Collections;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Web;
using  System.Web.SessionState;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.HtmlControls;
using  System.Data.SqlClient;
using  System.IO;

namespace  imageProcess
{
    
/// <summary>
    
/// ReadImage 的摘要说明。
    
/// </summary>

    public class ReadImage : System.Web.UI.Page
    
{
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
string strImageID = Request.QueryString["id"];
            SqlConnection myConnection 
= new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
            SqlCommand myCommand 
= new SqlCommand("Select ImageContentType, ImageData from ImageStore Where ImgID=" 
                
+ strImageID, myConnection);

            
try
            
{
                myConnection.Open();
                SqlDataReader myDataReader;
                myDataReader 
= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                
if(myDataReader.Read())
                
{
                    Response.Clear();
                    Response.ContentType 
= myDataReader["ImageContentType"].ToString();
                    Response.BinaryWrite((
byte[])myDataReader["ImageData"]);
                }

                myConnection.Close();
            }

            
catch (SqlException SQLexc)
            
{
                Response.Write(
"提取数据时出现错误:" + SQLexc.ToString());
            }

            Response.End();

        }


        
Web 窗体设计器生成的代码
    }

}

以上代码部分是应用自网络,与大家共同分享,谢谢!  

你可能感兴趣的:(数据库,datagrid,server,C#,存储,asp)