亚洲韩日午夜视频,欧美日韩在线精品一区二区三区,韩国超清无码一区二区三区,亚洲国产成人影院播放,久草新在线,在线看片AV色

您好,歡迎來到思海網(wǎng)絡(luò),我們將竭誠(chéng)為您提供優(yōu)質(zhì)的服務(wù)! 誠(chéng)征網(wǎng)絡(luò)推廣 | 網(wǎng)站備案 | 幫助中心 | 軟件下載 | 購(gòu)買流程 | 付款方式 | 聯(lián)系我們 [ 會(huì)員登錄/注冊(cè) ]
促銷推廣
客服中心
業(yè)務(wù)咨詢
有事點(diǎn)擊這里…  531199185
有事點(diǎn)擊這里…  61352289
點(diǎn)擊這里給我發(fā)消息  81721488
有事點(diǎn)擊這里…  376585780
有事點(diǎn)擊這里…  872642803
有事點(diǎn)擊這里…  459248018
有事點(diǎn)擊這里…  61352288
有事點(diǎn)擊這里…  380791050
技術(shù)支持
有事點(diǎn)擊這里…  714236853
有事點(diǎn)擊這里…  719304487
有事點(diǎn)擊這里…  1208894568
有事點(diǎn)擊這里…  61352289
在線客服
有事點(diǎn)擊這里…  531199185
有事點(diǎn)擊這里…  61352288
有事點(diǎn)擊這里…  983054746
有事點(diǎn)擊這里…  893984210
當(dāng)前位置:首頁 >> 技術(shù)文章 >> 文章瀏覽
技術(shù)文章

在SQL Server中保存和輸出圖片的具體方法

添加時(shí)間:2013-10-16 17:59:40  添加: 思海網(wǎng)絡(luò) 

 介紹
  有時(shí)候我們需要保存一些binary data進(jìn)數(shù)據(jù)庫(kù)。SQL Server提供一個(gè)叫做image的特殊數(shù)據(jù)類型供我們保存binary data。Binary data可以是圖片、文檔等。在這篇文章中我們將看到如何在SQL Server中保存和輸出圖片。
  
  建表
  為了試驗(yàn)這個(gè)例子你需要一個(gè)含有數(shù)據(jù)的table(你可以在現(xiàn)在的庫(kù)中創(chuàng)建它,也可以創(chuàng)建一個(gè)新的數(shù)據(jù)庫(kù)),下面是它的結(jié)構(gòu):
  
  Column Name
  Datatype
  Purpose
  
  ID
  Integer
  identity column Primary key
  
  IMGTITLE
  Varchar(50)
  Stores some user friendly title to identity the image
  
  IMGTYPE
  Varchar(50)
  Stores image content type. This will be same as recognized content types of ASP.NET
  
  IMGDATA
  Image
  Stores actual image or binary data.
  
  保存images進(jìn)SQL Server數(shù)據(jù)庫(kù)
  為了保存圖片到table你首先得從客戶端上傳它們到你的web服務(wù)器。你可以創(chuàng)建一個(gè)web form,用TextBox得到圖片的標(biāo)題,用HTML File Server Control得到圖片文件。確信你設(shè)定了Form的encType屬性為multipart/form-data。
  
  Stream imgdatastream = File1.PostedFile.InputStream;
  
  int imgdatalen = File1.PostedFile.ContentLength;
  
  string imgtype = File1.PostedFile.ContentType;
  
  string imgtitle = TextBox1.Text;
  
  byte[] imgdata = new byte[imgdatalen];
  
  int n = imgdatastream.Read(imgdata,0,imgdatalen);
  
  string connstr=
  
  ((NameValueCollection)Context.GetConfig
  
  ("appSettings"))["connstr"];
  
  SqlConnection connection = new SqlConnection(connstr);
  
  SqlCommand command = new SqlCommand
  
  ("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)
  
  VALUES ( @imgtitle, @imgtype,@imgdata )", connection );
  
  SqlParameter paramTitle = new SqlParameter
  
  ("@imgtitle", SqlDbType.VarChar,50 );
  
  paramTitle.Value = imgtitle;
  
  command.Parameters.Add( paramTitle);
  
  SqlParameter paramData = new SqlParameter
  
  ( "@imgdata", SqlDbType.Image );
  
  paramData.Value = imgdata;
  
  command.Parameters.Add( paramData );
  
  SqlParameter paramType = new SqlParameter
  
  ( "@imgtype", SqlDbType.VarChar,50 );
  
  paramType.Value = imgtype;
  
  command.Parameters.Add( paramType );
  
  connection.Open();
  
  int numRowsAffected = command.ExecuteNonQuery();
  
  connection.Close();
  
  從數(shù)據(jù)庫(kù)中輸出圖片
  現(xiàn)在讓我們從數(shù)據(jù)庫(kù)中取出我們剛剛保存的圖片,在這兒,我們將直接將圖片輸出至瀏覽器。你也可以將它保存為一個(gè)文件或做任何你想做的。
  
  private void Page_Load(object sender, System.EventArgs e)
  
  {
  
  string imgid =Request.QueryString["imgid"];
  
  string connstr=((NameValueCollection)
  
  Context.GetConfig("appSettings"))["connstr"];
  
  string sql="SELECT imgdata, imgtype FROM ImageStore WHERE id = "
  
  + imgid;
  
  SqlConnection connection = new SqlConnection(connstr);
  
  SqlCommand command = new SqlCommand(sql, connection);
  
  connection.Open();
  
  SqlDataReader dr = command.ExecuteReader();
  
  if(dr.Read())
  
  {
  
  Response.ContentType = dr["imgtype"].ToString();
  
  Response.BinaryWrite( (byte[]) dr["imgdata"] );
  
  }
  
  connection.Close();
  
  }
  在上面的代碼中我們使用了一個(gè)已經(jīng)打開的數(shù)據(jù)庫(kù),通過datareader選擇images。接著用Response.BinaryWrite代替Response.Write來顯示image文件。

關(guān)鍵字:Server、數(shù)據(jù)庫(kù)、圖片

分享到:

頂部 】 【 關(guān)閉
版權(quán)所有:佛山思海電腦網(wǎng)絡(luò)有限公司 ©1998-2024 All Rights Reserved.
聯(lián)系電話:(0757)22630313、22633833
中華人民共和國(guó)增值電信業(yè)務(wù)經(jīng)營(yíng)許可證: 粵B1.B2-20030321 備案號(hào):粵B2-20030321-1
網(wǎng)站公安備案編號(hào):44060602000007 交互式欄目專項(xiàng)備案編號(hào):200303DD003  
察察 工商 網(wǎng)安 舉報(bào)有獎(jiǎng)  警警  手機(jī)打開網(wǎng)站