[网络收集]asp.net存储图片到sql server数据库并显示

UploadImage.aspx文件

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadImage.aspx.cs" Inherits="_Default" %>

<!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>
    <TABLE RUNAT="server" WIDTH="700" ALIGN="left" ID="Table1" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD>上传图片(选择你要上传的图片)</TD>
<TD>
    <asp:FileUpload ID="UP_FILE" runat="server" />

</TD>
</TR>
<TR>
<TD>
</TD>
<TD>
     <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></TD>
</TR>
</TABLE>

    </div>
    </form>
</body>
</html>


UploadImage.aspx.cs文件

using System;
using System.Data;
using System.Configuration;
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.IO;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Int32 FileLength = UP_FILE.PostedFile.ContentLength;
        Byte[] myByteArray = new Byte[FileLength];

        Stream myStream = UP_FILE.PostedFile.InputStream;
        myStream.Read(myByteArray, 0, FileLength);

        SqlConnection conn = new SqlConnection("server=.;database=testdb;uid=sa;pwd=698887;");
        conn.Open();

        String sqlcmd = "insert into ImageStore(ImageData) values(@Image)";
        SqlCommand CmdObj = new SqlCommand(sqlcmd, conn);

        SqlParameter ImagePara = new SqlParameter();
        ImagePara.ParameterName = "@Image";
        ImagePara.SqlDbType = SqlDbType.Image;
        ImagePara.Value = myByteArray;
        CmdObj.Parameters.Add(ImagePara);
      
        CmdObj.ExecuteNonQuery();
        conn.Close();
        myStream.Close();


    }
}

 

ReadImage.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;
using System.Drawing;

public partial class ReadImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection("server=.;database=testdb;uid=sa;pwd=698887;");

        conn.Open();

        String sqlcmd = "select * from ImageStore where ImageID=7";
        SqlCommand cmd = new SqlCommand(sqlcmd,conn);
        SqlDataReader sqlReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        sqlReader.Read();
        byte[] ImageData = (byte[])sqlReader["ImageData"];
        Response.BinaryWrite(ImageData);


        Response.End();
        conn.Close();
    }
}

 

Default.aspx文件

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
        <asp:Image ID="Image1" runat="server" Height="115px" ImageUrl="~/ReadImage.aspx"
            Width="220px" /> </div>
    </form>
</body>
</html>

你可能感兴趣的:(SQL Server)