自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用.NET向SQL Server數(shù)據(jù)庫存取圖片

開發(fā) 后端
本文介紹了如何使用.NET向SQL Server數(shù)據(jù)庫存取圖片。使用ASP.NET上傳、讀取和顯示SQL Server數(shù)據(jù)庫里的圖片都十分方便。

使用.NET向SQL Server數(shù)據(jù)庫存取圖片

使用.Net技術(shù),我們可以很方便的將圖片存入SQL Server數(shù)據(jù)庫中并方便的讀取顯示出來,詳細(xì)的實(shí)現(xiàn)方法我們一步一步將會(huì)了解到。先說如何將圖片存儲(chǔ)到sql server數(shù)據(jù)庫中:

.NET向SQL Server數(shù)據(jù)庫存取圖片技巧:存入圖片

使用asp.net將圖片上傳并存入SQL Server中,然后從SQL Server中讀取并顯示出來:

1)上傳并存入SQL Server

數(shù)據(jù)庫結(jié)構(gòu)

  1. create table test   
  2. {   
  3. id identity(1,1),   
  4. FImage image   
  5. }   

相關(guān)的存儲(chǔ)過程

  1. Create proc UpdateImage   
  2. (   
  3. @UpdateImage Image   
  4. )   
  5. As   
  6. Insert Into test(FImage) values(@UpdateImage)   
  7. GO   

在UpPhoto.aspx文件中添加如下: 

  1. < input id="UpPhoto" name="UpPhoto" runat="server" type="file">   
  2. < asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上傳">< /asp:Button> 

然后在后置代碼文件UpPhoto.aspx.cs添加btnAdd按鈕的單擊事件處理代碼:

  1. private void btnAdd_Click(object sender, System.EventArgs e)   
  2. {   
  3. //獲得圖象并把圖象轉(zhuǎn)換為byte[]   
  4. HttpPostedFile upPhoto=UpPhoto.PostedFile;   
  5. int upPhotoLength=upPhoto.ContentLength;   
  6. byte[] PhotoArray=new Byte[upPhotoLength];   
  7. Stream PhotoStream=upPhoto.InputStream;   
  8. PhotoStream.Read(PhotoArray,0,upPhotoLength);   
  9. //連接數(shù)據(jù)庫   
  10. SqlConnection conn=new SqlConnection();   
  11. conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";   
  12. SqlCommand cmd=new SqlCommand("UpdateImage",conn);   
  13. cmd.CommandType=CommandType.StoredProcedure;   
  14. cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);   
  15. cmd.Parameters["@UpdateImage"].Value=PhotoArray;   
  16. //如果你希望不使用存儲(chǔ)過程來添加圖片把上面四句代碼改為:   
  17. //string strSql="Insert into test(FImage) values(@FImage)";   
  18. //SqlCommand cmd=new SqlCommand(strSql,conn);   
  19. //cmd.Parameters.Add("@FImage",SqlDbType.Image);   
  20. //cmd.Parameters["@FImage"].Value=PhotoArray;   
  21. conn.Open();   
  22. cmd.ExecuteNonQuery();   
  23. conn.Close();   
  24. }  

.NET向SQL Server數(shù)據(jù)庫存取圖片技巧:從SQL Server中讀取并顯示出來

在需要顯示圖片的地方添加如下代碼:

  1. < asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx">< /asp:image>  

ShowPhoto.aspx主體代碼:

  1. private void Page_Load(object sender, System.EventArgs e)   
  2. {   
  3. if(!Page.IsPostBack)   
  4. {   
  5. SqlConnection conn=new SqlConnection()   
  6. conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";   
  7. string strSql="select * from test where id=2";//這里假設(shè)獲取id為2的圖片   
  8. SqlCommand cmd=new SqlCommand(strSql,conn);   
  9. conn.Open();  
  10. SqlDataReader reader=cmd.ExecuteReader();  
  11. reader.Read();   
  12. Response.ContentType="application/octet-stream";   
  13. Response.BinaryWrite((Byte[])reader["FImage"]);   
  14. Response.End();   
  15. reader.Close();   
  16. }   

以上就介紹了.NET向SQL Server數(shù)據(jù)庫存取圖片的實(shí)現(xiàn)方法。

【編輯推薦】

  1. .NET二進(jìn)制圖片存儲(chǔ)與讀取的常見方法
  2. ASP.NET和SQL Server數(shù)據(jù)庫圖片存儲(chǔ)的實(shí)現(xiàn)
  3. ASP.NET數(shù)據(jù)庫圖片存儲(chǔ)到Sql2000中
  4. ASP.NET數(shù)據(jù)庫圖片上傳與讀取的實(shí)現(xiàn)
  5. ASP.NET(VB)應(yīng)用之圖片增加水印文字淺析
責(zé)任編輯:yangsai 來源: 網(wǎng)絡(luò)轉(zhuǎn)載
相關(guān)推薦

2011-07-22 14:22:43

java

2010-01-18 19:21:51

VB.NET存取數(shù)據(jù)庫

2010-06-17 11:08:07

SQL Server

2011-08-15 15:14:54

SQL Server存儲(chǔ)過程異常處理

2009-08-12 11:04:38

ASP.NET和SQL

2009-01-06 11:31:34

SybaseSQL Server數(shù)據(jù)庫

2010-07-16 11:24:59

SQL Server數(shù)

2011-05-20 13:11:22

ADO.NET

2011-07-18 10:45:55

C#SQL Server數(shù)

2010-07-15 17:28:50

SQL Server

2011-08-09 09:31:39

SQL Server數(shù)connectionS

2009-07-07 17:42:28

2011-03-28 12:33:09

SQL Server數(shù)據(jù)庫鏈接

2009-06-30 09:16:45

數(shù)據(jù)庫存儲(chǔ)JSP文件

2011-08-29 18:02:29

SQL Server FileStream

2011-08-11 09:12:31

SQL Server nolock

2009-07-31 10:29:57

ASP.NET數(shù)據(jù)庫操

2009-07-28 11:00:24

Excel導(dǎo)入SQL

2009-11-12 11:23:35

ADO.NET SQL

2010-07-08 11:05:14

SQL Server數(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)