动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   .Net → 阅读文章

 .Net在SqlServer中的图片存取技术

作者来源: 
阅读 1670 人次 , 2006-3-29 4:35:00 

本文总结如何在.net winform和.net webform(asp.net)中将图片存入sqlserver中并读取显示的方法
1,使用asp.net将图片上传并存入sqlserver中,然后从sqlserver中读取并显示出来
一,上传并存入sqlserver
数据库结构
create table test
{
id identity(1,1),
fimage image
}
相关的存储过程
create proc updateimage
(
@updateimage image
)
as
insert into test(fimage) values(@updateimage)
go

在upphoto.aspx文件中添加如下:
<input id="upphoto" name="upphoto" runat="server" type="file">
<asp:button id="btnadd" name="btnadd" runat="server" text="上传"></asp:button>

然后在后置代码文件upphoto.aspx.cs添加btnadd按钮的单击事件处理代码:
private void btnadd_click(object sender, system.eventargs e)
{
//获得图象并把图象转换为byte[]
httppostedfile upphoto=upphoto.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="data source=localhost;database=test;user id=sa;pwd=sa";

sqlcommand cmd=new sqlcommand("updateimage",conn);
cmd.commandtype=commandtype.storedprocedure;

cmd.parameters.add("@updateimage",sqldbtype.image);
cmd.parameters["@updateimage"].value=photoarray;

//如果你希望不使用存储过程来添加图片把上面四句代码改为:
//string strsql="insert into test(fimage) values(@fimage)";
//sqlcommand cmd=new sqlcommand(strsql,conn);
//cmd.parameters.add("@fimage",sqldbtype.image);
//cmd.parameters["@fimage"].value=photoarray;

conn.open();
cmd.executenonquery();
conn.close();
}

二,从sqlserver中读取并显示出来
在需要显示图片的地方添加如下代码:
<asp:image id="imgphoto" runat="server" imageurl="showphoto.aspx"></asp:image>

showphoto.aspx主体代码:
private void page_load(object sender, system.eventargs e)
{
if(!page.ispostback)
{
sqlconnection conn=new sqlconnection()
conn.connectionstring="data source=localhost;database=test;user id=sa;pwd=sa";

string strsql="select * from test where id=2";//这里假设获取id为2的图片
sqlcommand cmd=new sqlcommand()
reader.read();
response.contenttype="application/octet-stream";
response.binarywrite((byte[])reader["fimage"]);
response.end();
reader.close();
}
}

3,在winform中将图片存入sqlserver,并从sqlserver中读取并显示在picturebox中

1,存入sqlserver
数据库结构和使用的存储过过程,同上面的一样
1.1,在窗体中加一个openfiledialog控件,命名为ofdselectpic
1.2,在窗体上添加一个打开文件按钮,添加如下单击事件代码:
stream ms;
byte[] picbyte;
//ofdselectpic.showdialog();
if (ofdselectpic.showdialog()==dialogresult.ok)
{
if ((ms=ofdselectpic.openfile())!=null)
{
//messagebox.show("ok");
picbyte=new byte[ms.length];
ms.position=0;
ms.read(picbyte,0,convert.toint32(ms.length));
//messagebox.show("读取完毕!");

//连接数据库
sqlconnection conn=new sqlconnection();
conn.connectionstring="data source=localhost;database=test;user id=sa;pwd=sa";

sqlcommand cmd=new sqlcommand("updateimage",conn);
cmd.commandtype=commandtype.storedprocedure;

cmd.parameters.add("@updateimage",sqldbtype.image);
cmd.parameters["@updateimage"].value=picbyte;

conn.open();
cmd.executenonquery();
conn.close();

ms.close();
}
}

2,读取并显示在picturebox中
2.1 添加一个picturebox,名为ptbshow
2.2 添加一个按钮,添加如下响应事件:
sqlconnection conn=new sqlconnection();
conn.connectionstring="data source=localhost;database=test;user id=sa;pwd=sa";

string strsql="select fimage from test where id=1";

sqlcommand cmd=new sqlcommand(strsql,conn);

conn.open();
sqldatareader reader=cmd.executereader();
reader.read();

memorystream ms=new memorystream((byte[])reader["fimage"]);

image image=image.fromstream(ms,true);

reader.close();
conn.close();

ptbshow.image=image;

 本文Tags数据库  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:如何获得一个表的结构信息
· 下一篇:将一个图片按比例缩放显示在一个Frame中
· Asp.net 页面导航的几种方法与比较
· JRun常见问题回答
· DataRow 物件
· 一、Servlet和JSP概述
· 八、设置HTTP应答头


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077