如何从SQL server 2005中,读取图片,并输出在页面上,我在数据库中存储图片的字段的类型是varbinary?

ganmk 2008-05-07 09:58:46
如何从SQL server 2005中,读取图片,并输出在页面上,我在数据库中存储图片的字段的类型是varbinary?
...全文
573 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
nszbdycl 2011-12-09
  • 打赏
  • 举报
回复
不是很明白
jinjazz 2008-05-07
  • 打赏
  • 举报
回复
第一个字段存放文件大小,第二个字段存放数据
jinjazz 2008-05-07
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{
DownloadImage(Request["FileID"]);
}
private void DownloadImage(string FileID)
{
if (FileID == null || FileID == "") return;

using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection())
{
conn.ConnectionString = "server=.;uid=sa;pwd=;database=文件库";
conn.Open();

using (System.Data.SqlClient.SqlCommand command = conn.CreateCommand())
{

command.CommandText = string.Format("select * from 文件表 where id='{0}'", FileID);

System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader();

try
{
if (reader.Read())
{

int size = reader.GetInt32(0);
byte[] buffer = new byte[size];

reader.GetBytes(1, 0, buffer, 0, size);

this.Response.BinaryWrite(buffer);
}
}
catch (Exception ex)
{
this.Response.Write(ex.Message);
}
finally
{
reader.Close();
}
}
conn.Close();
}
}
yoshiki1895 2008-05-07
  • 打赏
  • 举报
回复
做一个页面

Response.ContentType = "";
Response.BinaryWrite(ImageContent);
Response.End();

然后把该页面用IMAGE控件显示出来
xiaozhimin1978 2008-05-07
  • 打赏
  • 举报
回复
显示的图片需要一个单独的aspx页面,在page_load方法中:

byte[] data = ......

this.Response.Clear();
this.Response.ContentType = "image/png";
this.Response.BinaryWrite(data);
this.Response.End();

其中 data 是你从数据库中读出来的二进制数据
wxg22526451 2008-05-07
  • 打赏
  • 举报
回复
http://tech.it168.com/KnowledgeBase/Articles/4/6/6/4666a843e52a0a81713930a4c585160f.htm

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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