怎么从SQL server中提出image对象,然后在image控件中显示???

wycaicai 2004-07-02 08:50:14
怎么从SQL server中提出image对象,然后在image控件中显示???
急问!
...全文
184 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wycaicai 2004-07-02
  • 打赏
  • 举报
回复
急!
wycaicai 2004-07-02
  • 打赏
  • 举报
回复
还没老大?
wycaicai 2004-07-02
  • 打赏
  • 举报
回复
老大们,解决一下拉,急!
appleblossom 2004-07-02
  • 打赏
  • 举报
回复
这个问题好,关注中.
wycaicai 2004-07-02
  • 打赏
  • 举报
回复
我是另外做的一个专门显示图片的页面,在原来的页面中有个image,起imageUrl设置为这个aspx页面,但是没有反映?
急!!!
若白师妹 2004-07-02
  • 打赏
  • 举报
回复
你要另外单独做一个专门显示图片的ASPX页面,然后在里面进行Response.BinaryWrite(image);的操作,然后把Image控件的URL设置为这个Aspx页面
wycaicai 2004-07-02
  • 打赏
  • 举报
回复
我的做法不知道哪个地方有问题,但没报错!
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//int strImageID =4;
string strImageID = Request.QueryString["id"];
SqlConnection myConnection = new SqlConnection("Data Source=.;Initial Catalog=Test_image;User Id=sa;Password=;");
SqlCommand myCommand = new SqlCommand("Select picture,type from pic Where id="+strImageID, myConnection);

try
{
myConnection.Open();
SqlDataReader myDataReader;
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if(myDataReader.Read())
{
Response.Clear();

Response.ContentType =myDataReader["type"].ToString();
byte[] image;
image=(byte[])myDataReader["picture"];
if (image.Length !=0)
Response.BinaryWrite(image);
//Response.WriteFile(@"E:\myself\照片\wycc.jpg");

}
myConnection.Close();
}
catch (SqlException SQLexc)
{
}
Response.End();

}
wycaicai 2004-07-02
  • 打赏
  • 举报
回复
还没老大解决一下啊!
在线等啊!
wycaicai 2004-07-02
  • 打赏
  • 举报
回复
算了,我自己解决了
问题是我在把相片(二进制文件)放到数据库中的时候,用的是存储过程,参数DataType.image后面写的是16,而二进制文件是8位的byte所以不符合。产生输出来的二进制文件有问题,把16改成8或者不要,即可解决!!!!
Firestone2003 2004-07-02
  • 打赏
  • 举报
回复
我的解决方法:
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace BSSystem.files.member
{
/// <summary>
/// DisplayImage 的摘要说明。
/// </summary>
public class DisplayImage : System.Web.UI.Page
{


protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;

private int MyID;
private SqlCommand myCommand;
private SqlConnection myConn;
private SqlDataReader myDataReader;
private string strConn=System.Configuration.ConfigurationSettings.AppSettings.Get("MySqlConnection.ConnectionString");
protected string imagepath=System.Configuration.ConfigurationSettings.AppSettings.Get("imagepath");

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(this.Session["userUnit"].ToString()=="")
{
Response.Write("非法访问,请重新登陆!");
Response.End();
return;
}


if(Request.QueryString["ID"]!=null)
{
if(Request.QueryString["ID"].ToString()!="")
{
MyID=int.Parse(Request.QueryString["ID"]);
}
else
{
Response.Write("传入参数出错!");
Response.End();
}
}
else
{
Response.Write("传入参数出错!");
Response.End();
}

this.myCommand.CommandText="select PersonnelPhoto,PersonnelPhotoSize,PersonnelPhotoType from Personnel where ID="+MyID.ToString();
myConn.Open();
this.myDataReader=this.myCommand.ExecuteReader();
if(this.myDataReader.Read())
{
Response.ContentType=this.myDataReader["PersonnelPhotoType"].ToString();
Response.OutputStream.Write((byte[])myDataReader["PersonnelPhoto"], 0, int.Parse(myDataReader["PersonnelPhotoSize"].ToString()));//这一句不同
Response.End();
}

this.myDataReader.Close();
this.myConn.Close();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
myCommand=new SqlCommand();
myConn=new SqlConnection();
myConn.ConnectionString=strConn;
myCommand.Connection=this.myConn;
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}


Response.OutputStream.Write((byte[])myDataReader["PersonnelPhoto"], 0, int.Parse(myDataReader["PersonnelPhotoSize"].ToString()));//这一句不同

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧