欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET數(shù)據(jù)庫存取圖片的方法

 更新時間:2017年01月24日 16:41:38   作者:Yesi  
這篇文章主要為大家詳細(xì)介紹了ASP.NET數(shù)據(jù)庫如何存取圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

當(dāng)我們有大量的圖片或者圖片比較大時,我們常規(guī)的做法可能是保存圖片路徑,但是也不排除需要將圖片直接存放到數(shù)據(jù)庫的情況,此時就需要保存圖片到數(shù)據(jù)庫了。這篇文章我會向大家介紹:

如何通過FileUpLoad控件將圖片保存到數(shù)據(jù)庫
如何通過Button控件從數(shù)據(jù)庫導(dǎo)出圖片

具體步驟如下:

保存圖片到數(shù)據(jù)庫

第一步:首先在數(shù)據(jù)庫創(chuàng)建一個名為“Images”的表,代碼如下:

CREATE TABLE Images
( 
Roll_no varchar(12) primary key,
Name_File varchar(100),
Extension varchar(100) , 
img varbinary(max) ,
Img_date datetime 
) 

可以看到這個表存儲了這些內(nèi)容:圖片的登記號、文件名、文件擴(kuò)展名、二進(jìn)制數(shù)據(jù)以及上傳時間。

第二步:然后打開Visual Studio,創(chuàng)建一個空網(wǎng)站,命名為”ImageToBinary”。

第三步:再添加一個新頁面,命名為“Conversion.aspx”

在這個頁面我們拖進(jìn)TextBox , FileUpload, Button這三個控件。

界面如圖:

當(dāng)然你也可以選擇在Conversion.apsx文件直接輸入這串代碼:

文件序號
<asp:TextBox ID="txtrollno" runat="server">
</asp:TextBox>
<br />

選擇文件
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />

<asp:Button ID="Button1" runat="server"
Text="上傳" OnClick="Button1_Click" />

第四步:控件添加后,雙擊Button,進(jìn)入Conversion.apxs.cs文件,添加以下命名空間:

using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web; 

然后在Button1_Click內(nèi)編寫代碼,將圖片轉(zhuǎn)換為二進(jìn)制流并通過SQL語句保存到數(shù)據(jù)庫中。

代碼如下:

protected void Button1_Click(object sender, EventArgs e)
{
 if (!FileUpload1.HasFile) 
  {
  Response.Write("未選擇文件");
  return;
  }


  else
  {
  //創(chuàng)建訪問上傳文件的對象,并獲取上傳的文件
  HttpPostedFile file = FileUpload1.PostedFile;

  //獲取上傳文件的文件名和擴(kuò)展名
  string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); 
  string extension = Path.GetExtension(filename); 
  
  //實(shí)例化一個byte數(shù)組,其長度等于上傳文件的長度
  byte[] imagetype = new byte[file.ContentLength];
  
  //將文件數(shù)據(jù)讀取到byte數(shù)組中
  file.InputStream.Read(imagetype, 0, file.ContentLength); 

  //判斷圖片格式
  if ((extension == ".jpg") || (extension == ".png") || (extension == ".gif") || (extension == ".bmp"))
  {
   //表里寫入數(shù)據(jù)
   using (SqlConnection connection = new SqlConnection("Data Source=AFOD3-609221015;Initial Catalog=MyData;Integrated Security=True"))
   {

   connection.Open();
   SqlCommand cmd = new SqlCommand();
   cmd.Connection = connection;

   string commandText = "Insert into Images values (@image, @Rollno,@img,getdate())";

   cmd.CommandText = commandText;
   cmd.CommandType = CommandType.Text;

   cmd.Parameters.Add("@image", SqlDbType.VarBinary);
   cmd.Parameters["@image"].Value = imagetype;

   cmd.Parameters.Add("@Rollno", SqlDbType.VarChar);
   cmd.Parameters["@Rollno"].Value = txtrollno.Text;

   cmd.Parameters.Add("@img", SqlDbType.VarChar);
   cmd.Parameters["@img"].Value = txtrollno.Text;


   cmd.ExecuteNonQuery();
   cmd.Dispose();
   connection.Close();

   Response.Write("導(dǎo)入成功");
   }
  }
  else
  {
   Response.Write("導(dǎo)入失敗"); return;
  }
  }

運(yùn)行結(jié)果如圖:

這時我們就可以瀏覽文件夾添加需要存入的圖片:

文件成功導(dǎo)入

如果選擇了不符合條件的文件后,顯示結(jié)果:

返回?cái)?shù)據(jù)庫,可以看到圖片已經(jīng)成功添加到數(shù)據(jù)庫中了:

導(dǎo)出圖片

現(xiàn)在我們看如何從數(shù)據(jù)庫導(dǎo)出圖片的,這里我會只使用Button控件,簡單概述一下。

第一步:在Visual Studio創(chuàng)建一個空網(wǎng)站,命名為“ImageToBinary”。

第二步:再添加一個新頁面,命名為"GetImage.aspx"。在這個頁面拖放一個Button控件。

第三步: 雙擊Button,進(jìn)入”GetImage.aspx.cs”,添加命名空間。

using System;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;

Button1_Click內(nèi)編寫代碼:

protected void Button1_Click(object sender, EventArgs e)
 {
 string sConn = ConfigurationManager.AppSettings["ConnectionString"];

 SqlConnection objConn = new SqlConnection(sConn);
 
 objConn.Open();

 string sql = "select * from Images";

 SqlCommand cmd = new SqlCommand(sql, objConn);
 
 SqlDataReader dr = cmd.ExecuteReader();

 while (dr.Read())

 {

  byte[] bytes = (byte[])dr["img"];

  FileStream fs = new FileStream(@"E:\Images\" + dr["roll_no"] + ".jpg" , FileMode.Create, FileAccess.Write);

  fs.Write(bytes, 0, bytes.Length);

  fs.Flush();

  fs.Close();

 }

 dr.Close();

 objConn.Close();
 
 Response.Write("成功導(dǎo)出"); 


 }

運(yùn)行結(jié)果:

點(diǎn)擊“導(dǎo)出”:

打開指定的文件夾,圖片已經(jīng)保存在里面了:

最后,如果有需要,你還可以參考這篇文章:如何保存PDF、Word和Excel文件到數(shù)據(jù)庫中

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論