。net怎么把数据库中的二进制图片绑定到datalist中 在sql数据库中图片存的是二进制的,Image类型的,怎样才能把它绑定到datalist中并显示?各位高手请帮帮忙!!!万分感激!!!QQ:72728228 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Image类型 为什么非要是这个类型啊 varchar不行么!!!数据库存图片的名称不好吗 后台直接绑定就是了 http://www2.ccw.com.cn/tips/9905/051701_04.asp. 我存的就是Image类型啊,怎么弄啊? 那要怎么绑定到datalist 中啊?可以给点代码看看吧? 我晕!baidu查一下序列化不就完了,你反序列化出来之后写个Eval绑定不就完了 你的真正的图片应该是放在你的网站里面右面资源管理器的images(这里可以自己建)里面。而你在数据库里面只是有一个二维表,而这个二维表的其中一个属性列就是记录图片所放的URL地址(一般写成相对路径,对应的是网站上图片放的位置,这里不能搞错,相对路径的形式:|images\1.gif),然后让这个二维表中想要绑定的属性列(这时肯定包括图片的那一属性列)绑定到datalist上,运行应该就能把图片显示出来的。 新建页面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); } }} GridView CommandField delete问题 session在远程服务器失效 ASPNET 2.0新功能 询问 GridView中删除问题 ASP页面如何迁移到.NET2005 如何获取datalist页眉模版中的label的text值 50分!~~~在线等!! .net 域验证 后台接受前台confirm() 的值是true,还是false JS中如何修改鼠标光标的形状 gredview与checkbox组合标题显示的问题 求一正则表达式
数据库存图片的名称不好吗 后台直接绑定就是了
<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);
}
}
}