.Net在SqlServer中的图片存取技术
2024-07-10 12:57:20
供稿:网友
 
本文总结如何在.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;