SQL Server2005存储图片的一般方式 及其程序调用特点~最好能带上建表 和 插入测试数据的脚本 thanks~~

解决方案 »

  1.   

    邹老大的代码可以参考一下:http://blog.csdn.net/htl258/archive/2010/04/15/5486178.aspx
      

  2.   

    买了本周老大的sql 秘籍,还未研究。
    建议还是存路径吧
      

  3.   

    以前我们做得比较多的是存储路径 然后写程序调用 
    2005和2008可以用文件流也可以存储成二进制  varbinary格式的
      

  4.   

    将图片直接转换成二进制,存入数据库以及从数据库中取出显示在页面上。UpPhoto.aspx:
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="UpPhoto.aspx.cs" Inherits="_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 id="Head1" runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="UpPhoto1" name="UpPhoto1" runat="server" type="file" />
            <asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上传" 
                onclick="btnAdd_Click"></asp:Button> 
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br/><br/>
            <asp:Button ID="btnShow" runat="server" onclick="btnShow_Click" Text="显示"/>
            <br/>
            <asp:image id="imgPhoto" runat="server" ImageUrl="UpPhoto.aspx" 
                Height="157px" Width="185px"></asp:image> 
        </div>
        </form>
    </body>
    </html>UpPhoto.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.Data.SqlClient;
    using System.IO;public partial class _UpPhoto : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {     }    //上传图片按钮    protected void btnAdd_Click(object sender, EventArgs e)
        {
            //获得图象并把图象转换为byte[] 
            HttpPostedFile upPhoto = UpPhoto1.PostedFile;
            int upPhotoLength = upPhoto.ContentLength;
            byte[] PhotoArray = new Byte[upPhotoLength];
            Stream PhotoStream = upPhoto.InputStream;
            PhotoStream.Read(PhotoArray, 0, upPhotoLength);
            //连接数据库 
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Server=.;uid=sa;pwd=123456;database=pictest";
            string strSql=string.Format("Insert into db_image(FImage) values(@FImage)");
            SqlCommand cmd = new SqlCommand(strSql, conn);
            cmd.Parameters.Add("@FImage", SqlDbType.Image);
            cmd.Parameters["@FImage"].Value = PhotoArray;        conn.Open();        try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
               if (conn != null)
               {
                   conn.Close();
               }         }       
        }   //显示图片按钮    protected void btnShow_Click(object sender, EventArgs e)
        {
            
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = "Server=.;uid=sa;pwd=123456;database=pictest";            //显示文本框中输入的图片            string strSql = "select * from db_image where id='"+TextBox1.Text+"'";            conn.Open();
                SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
                DataSet ds = new DataSet();
                sda.Fill(ds); 
                int ID = Convert.ToInt32(ds.Tables[0].Rows[0]["id"]);
                this.imgPhoto .ImageUrl = "pic.aspx?id=" + ID + "";//转向另一页面
                conn.Close();//关闭连接    }
    }图片如果想直接在上传当前页显示,要走个曲线救国的路线 。即显示在新打开的页面,将新打开的页面显示在上传图片页的Image控件上。pic.aspx:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="pic.aspx.cs" Inherits="pic" %><!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>pic.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 pic : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int id;
            int.TryParse(this.Request.QueryString["id"], out id);//获取传过来图片ID参数
            if (id != 0)
            {
                int imgID = Convert.ToInt32(Request.QueryString["id"]);
                SqlConnection con = new SqlConnection("Server=.;uid=sa;pwd=123456;database=pictest");//加接数据库
                con.Open();//打开连接
                string sql = "select * from db_image where id='"+ imgID +"'";
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataReader reader = cmd.ExecuteReader();
                try
                {
                    
                    reader.Read();
                    Response.ContentType = "application/octet-stream";
                    Response.BinaryWrite((Byte[])reader["FImage"]);
                    Response.End();
                    reader.Close();
                }
                catch (Exception ex)
                {
                    Response.Write(ex);//显示错误信息
                }
            }    }
    }
      

  5.   

    图片保存到数据库的方法
    http://topic.csdn.net/u/20081024/11/846e3e56-218b-4b07-b733-ee87dc2fe687.html?882064923
      

  6.   

    上传
    HttpPostedFile upPhoto = UpPhoto.PostedFile;
            int upPhotoLength = upPhoto.ContentLength;
            byte[] PhotoArray = new Byte[upPhotoLength];
            Stream PhotoStream = upPhoto.InputStream;
            PhotoStream.Read(PhotoArray, 0, upPhotoLength);
            //连接数据库
            string constr = "Data Source=.;Initial Catalog=test;Integrated Security=True";
            SqlConnection conn = new SqlConnection(constr);
            string str = "insert into images values(@pic)";
            SqlCommand cmd = new SqlCommand(str, conn);
            cmd.Parameters.Add("@pic", SqlDbType.Image);
            cmd.Parameters["@pic"].Value = PhotoArray;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            Response.Write("图片上传成功");获取图片            string connstr = "Data Source=.;Initial Catalog=test;Integrated Security=True";
                SqlConnection conn = new SqlConnection(connstr);
                conn.Open();
                string sql="select id,img from images";
                SqlDataAdapter dp = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                dp.Fill(ds, "dd");
                dlContent.DataSource = ds;
                dlContent.DataBind();
                conn.Close();
    显示
     protected void Page_Load(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            ShowPic(id);    }
        private void ShowPic(int id)
        {
            //连接数据库 
            string ConnStr = "Data Source=.;Initial Catalog=test;Integrated Security=True";
            string strSql = "select * from images where id='" + id + "'";
            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["img"]);
                Response.Write("successful");
            }
            reader.Close();
            conn.Close();
            Response.End();
        } 
      

  7.   

    http://www.knowsky.com/3257.html