WEB开发网
开发学院WEB开发ASP.NET asp.net存储图片到sql server数据库并显示 阅读

asp.net存储图片到sql server数据库并显示

 2010-11-09 13:03:06 来源:WEB开发网   
核心提示:SQL Server数据库简体繁体数据混用的问题http://tech.cncms.com/shujuku/mssql/100037.htmlSQL Server数据库对于应用程序的关系UploadImage.aspx文件<%@ Page Language="C#" AutoEventWire

SQL Server数据库简体繁体数据混用的问题

http://tech.cncms.com/shujuku/mssql/100037.html

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>龙振升(爽爽)QQ:749081771_www.15597.com</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>爽爽-7wy.net</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>

1 2  下一页

Tags:asp.net 存储图片 sql server数据库

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接