在sql数据库中图片存的是二进制的,Image类型的,怎样才能把它绑定到datalist中并显示?各位高手请帮帮忙!!!万分感激!!!QQ:72728228

解决方案 »

  1.   

    Image类型  为什么非要是这个类型啊 varchar不行么!!!
    数据库存图片的名称不好吗  后台直接绑定就是了
      

  2.   

    http://www2.ccw.com.cn/tips/9905/051701_04.asp.
      

  3.   

    我存的就是Image类型啊,怎么弄啊?
      

  4.   

    那要怎么绑定到datalist 中啊?可以给点代码看看吧?
      

  5.   

    我晕!baidu查一下序列化不就完了,你反序列化出来之后写个Eval绑定不就完了
      

  6.   

    你的真正的图片应该是放在你的网站里面右面资源管理器的images(这里可以自己建)里面。而你在数据库里面只是有一个二维表,而这个二维表的其中一个属性列就是记录图片所放的URL地址(一般写成相对路径,对应的是网站上图片放的位置,这里不能搞错,相对路径的形式:|images\1.gif),然后让这个二维表中想要绑定的属性列(这时肯定包括图片的那一属性列)绑定到datalist上,运行应该就能把图片显示出来的。
      

  7.   

    新建页面1为:ImgPlay.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImgPlay.aspx.cs" Inherits="ImgPlay" %><!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:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
                <ItemTemplate>
                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("PhotoID","~/DisplayPhoto.aspx?PhotoID={0}") %>' /><br />
                    <br />
                </ItemTemplate>
            </asp:DataList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringImg %>"
                SelectCommand="SELECT [PhotoID] FROM [Photos]"></asp:SqlDataSource>
        </div>
        </form>
    </body>
    </html>新建页面2为:DisplayPhoto.aspx页面内容为:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DisplayPhoto.aspx.cs" Inherits="DisplayPhoto" %>DisplayPhoto.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.IO;
    using System.Data.SqlClient;public partial class DisplayPhoto : System.Web.UI.Page
    {
        int PhotoID; //图片id
        protected void Page_Load(object sender, EventArgs e)
        {
           if (this.Request.QueryString["PhotoID"] != null)
            {            
                PhotoID = int.Parse(this.Request.QueryString["PhotoID"].ToString());
                PhotoID = (int) PhotoID;            IDImgView();
            }
        }    protected void IDImgView()
        {
            string connString = ConfigurationManager.ConnectionStrings["ConnectionStringImg"].ConnectionString;        SqlConnection connection = new SqlConnection(connString);        string sql = "SELECT [图片字段]  FROM Photos WHERE PhotoID = " + TKPhotoID + "";        SqlCommand command = new SqlCommand(sql, connection);
            command.Parameters.Add(new SqlParameter("@PhotoID", TKPhotoID));        connection.Open();        Stream stream = null;
            object result = command.ExecuteScalar();
            stream = new MemoryStream((byte[])result);
            const int buffersize = 1024 * 16;
            byte[] buffer = new byte[buffersize];
            int count = stream.Read(buffer, 0, buffersize);
            while (count > 0)
            {
                Response.OutputStream.Write(buffer, 0, count);
                count = stream.Read(buffer, 0, buffersize);
            }
            
        }
    }