當我們有大量的圖片或者圖片比較大時,我們常規的做法可能是儲存圖片路徑,但是也不排除需要將圖片直接存放到資料庫的情況,此時就需要儲存圖片到資料庫了。這篇文章我會向大家介紹: 如何通過FileUpLoad控制項將圖片儲存到資料庫 如何通過Button控制項從資料庫匯出圖片
具體步驟如下:
儲存圖片到資料庫
第一步:首先在資料庫建立一個名為“Images”的表,代碼如下:
CREATE TABLE Images( Roll_no varchar(12) primary key,Name_File varchar(100),Extension varchar(100) , img varbinary(max) ,Img_date datetime )
可以看到這個表格儲存體了這些內容:圖片的登記號、檔案名稱、副檔名、位元據以及上傳時間。
第二步:然後開啟Visual Studio,建立一個空網站,命名為”ImageToBinary”。
第三步:再添加一個新頁面,命名為“Conversion.aspx”
在這個頁面我們拖進TextBox , FileUpload, Button這三個控制項。
介面如圖:
當然你也可以選擇在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,進入Conversion.apxs.cs檔案,添加以下命名空間:
using System;using System.Data;using System.Data.SqlClient;using System.IO;using System.Web;
然後在Button1_Click內編寫代碼,將圖片轉換為二進位流並通過SQL語句儲存到資料庫中。
代碼如下:
protected void Button1_Click(object sender, EventArgs e){ if (!FileUpload1.HasFile) { Response.Write("未選擇檔案"); return; } else { //建立訪問上傳檔案的對象,並擷取上傳的檔案 HttpPostedFile file = FileUpload1.PostedFile; //擷取上傳檔案的檔案名稱和副檔名 string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); string extension = Path.GetExtension(filename); //執行個體化一個byte數組,其長度等於上傳檔案的長度 byte[] imagetype = new byte[file.ContentLength]; //將檔案資料讀取到byte數組中 file.InputStream.Read(imagetype, 0, file.ContentLength); //判斷圖片格式 if ((extension == ".jpg") || (extension == ".png") || (extension == ".gif") || (extension == ".bmp")) { //表裡寫入資料 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("匯入成功"); } } else { Response.Write("匯入失敗"); return; } } View Code
運行結果如圖:
這時我們就可以瀏覽檔案夾添加需要存入的圖片:
檔案成功匯入
如果選擇了不合格檔案後,顯示結果:
返回資料庫,可以看到圖片已經成功添加到資料庫中了:
匯出圖片
現在我們看如何從資料庫匯出圖片的,這裡我會只使用Button控制項,簡單概述一下。
第一步:在Visual Studio建立一個空網站,命名為“ImageToBinary”。
第二步:再添加一個新頁面,命名為"GetImage.aspx"。在這個頁面拖放一個Button控制項。
第三步: 雙擊Button,進入”GetImage.aspx.cs”,添加命名空間。
using System;using System.Configuration;using System.Data.SqlClient;using System.IO;
Button1_Click內編寫代碼:
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("成功匯出"); } View Code
運行結果:
點擊“匯出”:
開啟指定的檔案夾,圖片已經儲存在裡面了:
最後,如果有需要,你還可以參考這篇文章:如何儲存PDF、Word和Excel檔案到資料庫中
謝謝瀏覽。