.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;