把图片以二进制方式存入sql server数据库并读取出来(.net)

 
1、建所需数据库和表,语句如下:

--建立数据库
create database test

--使用该数据库
use test

--建立存放图片的表
create table piclist(
id int Identity primary key,
    pic Image not null
)

2、制作上传图片的模块,代码如下:
前台html代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpPhoto.aspx.cs" Inherits="Test_UpPhoto" %>

<!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>
            <input id="UpPhoto" name="UpPhoto" runat="server" type="file" />
            <asp:Button id="btnAdd" runat="server" Text="上传" OnClick="btnAdd_Click"></asp:Button>
    </div>
    </form>
</body>
</html>

后台代码:
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.IO;
using System.Data.SqlClient;

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

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //获得图象并把图象转换为byte[]
        HttpPostedFile upPhoto = UpPhoto.PostedFile;
        int upPhotoLength = upPhoto.ContentLength;
        byte[] PhotoArray = new Byte[upPhotoLength];
        Stream PhotoStream = upPhoto.InputStream;
        PhotoStream.Read(PhotoArray, 0, upPhotoLength);

        //连接数据库
        string ConStr = "server=(local);user id=sa;pwd=sa;database=test";
        SqlConnection conn = new SqlConnection(ConStr);       

        string strSql = "Insert into piclist(pic) values(@pic)";
        SqlCommand cmd = new SqlCommand(strSql, conn);
        cmd.Parameters.Add("@pic", SqlDbType.Image);
        cmd.Parameters["@pic"].Value = PhotoArray;

        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        Response.Write("图片上传成功");
    }
}

3、制作显示图片的模块:
后台代码:
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 Test_ShowPhoto : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         if(!Page.IsPostBack)
        {
            //连接数据库
            string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
            string strSql = "select * from piclist";
            SqlConnection conn = new SqlConnection(ConnStr);
            conn.Open();

            SqlCommand cmd=new SqlCommand(strSql,conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Response.ContentType = "application/octet-stream";
                Response.BinaryWrite((Byte[])reader["pic"]);
                Response.Write("successful");
            }
             reader.Close();
             conn.Close();
             Response.End();
        }
    }
}
posted @ 2010-06-29 16:29  Daniel_Lu  阅读(273)  评论(0编辑  收藏  举报