国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

ASP.NET中讓圖片以二進(jìn)制的形式存儲(chǔ)在數(shù)據(jù)庫(kù)中

2019-11-14 16:31:54
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

    今早有個(gè)網(wǎng)友問(wèn)到我這問(wèn)題,以前我都是直接在數(shù)據(jù)庫(kù)中存文件名的,還沒(méi)有試過(guò)存儲(chǔ)整張圖片到數(shù)據(jù)庫(kù)中,上網(wǎng)搜索了一下,自己又測(cè)試了一番,代碼如下:
建立保存圖片的表的SQL語(yǔ)句:

 

Sql代碼  收藏代碼
  1. USE [niunantest]  
  2. GO  
  3. /****** 對(duì)象:  Table [dbo].[picdata]    腳本日期: 03/30/2010 14:51:58 ******/  
  4. SET ANSI_NULLS ON  
  5. GO  
  6. SET QUOTED_IDENTIFIER ON  
  7. GO  
  8. CREATE TABLE [dbo].[picdata](  
  9.     [id] [int] IDENTITY(1,1) NOT NULL,  
  10.     [content] [image] NULL,  
  11.     [createdate] [datetime] NOT NULL CONSTRAINT [DF_picdata_createdate]  DEFAULT (getdate()),  
  12.  CONSTRAINT [PK_picdata] PRIMARY KEY CLUSTERED   
  13. (  
  14.     [id] ASC  
  15. )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]  
  16. ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]  

 

下面是保存圖片到數(shù)據(jù)庫(kù)中的代碼片段:

 

C#代碼  收藏代碼
  1. int len = fu.PostedFile.ContentLength;  // 圖片大小  
  2. byte[] pic = new byte[len];  // 創(chuàng)建一個(gè)字節(jié)數(shù)組,大小為圖片的大小,數(shù)據(jù)庫(kù)中就存儲(chǔ)這個(gè)東西  
  3. fu.PostedFile.InputStream.Read(pic, 0, len); // 把上傳控件中的文件用二進(jìn)制讀取存到pic字節(jié)數(shù)組中  
  4. //   插入圖片到數(shù)據(jù)庫(kù)中       
  5. SqlConnection connection = new  
  6. SqlConnection(@"server=./sqlexpress;database=niunantest;uid=sa;pwd=123456");  
  7. try  
  8. {  
  9.     connection.Open();  
  10.     SqlCommand cmd = new SqlCommand("insert   into   picdata   "  
  11.     + "([content])   values   (@pic)", connection);  
  12.     cmd.Parameters.Add("@pic", pic);  
  13.     cmd.ExecuteNonQuery();  
  14.     Label1.Text = "圖片插入數(shù)據(jù)庫(kù)成功!";  
  15.   
  16.     Image1.ImageUrl = "getpic.ashx?t=" + DateTime.Now.Ticks;  // 顯示剛剛插入數(shù)據(jù)庫(kù)的圖片  
  17. }  
  18. finally  
  19. {  
  20.     connection.Close();  
  21. }   

 

下面是從數(shù)據(jù)庫(kù)中取出圖片的代碼片段:

 

C#代碼  收藏代碼
  1. MemoryStream stream = new MemoryStream();  
  2. SqlConnection connection = new  
  3. SqlConnection(@"server=./sqlexpress;database=niunantest;uid=sa;pwd=123456");  
  4. try  
  5. {  
  6.     connection.Open();  
  7.     SqlCommand command = new  
  8.     SqlCommand("select top 1  [content]   from   picdata order by id desc", connection);  
  9.     byte[] image = (byte[])command.ExecuteScalar();  
  10.     stream.Write(image, 0, image.Length);  
  11.     Bitmap bitmap = new Bitmap(stream);  
  12.     context.Response.ContentType = "image/jpeg";  
  13.     bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);  
  14. }  
  15. finally  
  16. {  
  17.     connection.Close();  
  18.     stream.Close();  
  19. }   

    其實(shí)也就是通過(guò)流把圖片搞成字節(jié)數(shù)組再存到數(shù)據(jù)庫(kù)中,然后再?gòu)臄?shù)據(jù)庫(kù)中讀取字節(jié)數(shù)組出來(lái),再通過(guò)字節(jié)數(shù)組創(chuàng)建流,再通過(guò)流把圖像輸出出來(lái),發(fā)現(xiàn)你存到數(shù)據(jù)庫(kù)中的是gif圖像的話再取出來(lái)是可以把他轉(zhuǎn)為jpg的圖像的,因?yàn)樵谌〕鰣D像的時(shí)候我們?cè)O(shè)置他的ContentType是image/jpeg了。


源碼下載:http://niunan.net/download/picsave2db.7z


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黎川县| 义马市| 五寨县| 西城区| 沾化县| 沂南县| 乐陵市| 河北省| 吴忠市| 兴海县| 桑植县| 平山县| 金湖县| 凭祥市| 洛浦县| 昌邑市| 玉林市| 铜川市| 中西区| 盘锦市| 荔浦县| 武平县| 北辰区| 东方市| 大安市| 洛浦县| 孝感市| 三江| 剑河县| 潮州市| 铜鼓县| 灌阳县| 奈曼旗| 循化| 泊头市| 保德县| 桃园市| 漾濞| 孝昌县| 鞍山市| 介休市|