在數據庫中 存儲圖片 以及 在界面中顯示圖片(存儲圖片路徑)- 這種方法相對與存儲二進制文件好


花了一下午時間,終於學會了在數據庫中存儲圖片,以及在界面中顯示圖片了。

存儲圖片有兩種方法:

一種是:直接把圖片轉換成二進制文件存儲在數據庫中。

一種是:存儲圖片的路徑到數據庫,用的時候直接調用路徑給image等圖像控件即可。

兩種方法,有利有弊,第一種方法適合存儲量少且重要的圖片信息。第二種方法適合存儲量大但不是太重要的圖片。

我使用的是第二種方法,簡單,實用。

 

下面就是我具體的實現過程:

 

首先我新建了兩個網頁文件,insert_photo.aspx / select_photo.apsx

第一個用來往數據庫中存儲照片,第二個是在網頁中顯示照片。

 

這是第一個網頁文件的前台代碼:

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="photo_path.aspx.cs" Inherits="photo_path" Debug ="true"%>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12     <div>
13         圖片ID:<asp:TextBox ID="txt_id" runat="server"></asp:TextBox>&nbsp;&nbsp;
14         圖片描述:<asp:TextBox ID="txt_mark" runat="server"></asp:TextBox>&nbsp;&nbsp;
15         <asp:FileUpload ID="inputfile" runat="server" />&nbsp;&nbsp;
16         <asp:Button ID="Button1" runat="server" Text="上傳" OnClick ="Button1_Click"/>&nbsp;&nbsp;
17         <asp:Label ID="Label1" runat="server"></asp:Label>
18     </div>
19     </form>
20 </body>
21 </html>

 

這是第一個網頁文件的后台代碼:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 using System.Data;
 9 using System.Configuration;
10 using System.Data.SqlClient;
11 using System.IO;
12 
13 public partial class photo_path : System.Web.UI.Page
14 {
15     protected void Page_Load(object sender, EventArgs e)
16     {
17     }
18 
19     protected void Button1_Click(object sender, EventArgs e)
20     {
21         #region 用於把圖片保存到網站指定的文件夾。
22         string filepath = HttpContext.Current.Server.MapPath("~/img/");
23 
24         string filefullname = filepath + inputfile.FileName;
25 
26         inputfile.SaveAs(filefullname);
27         #endregion
28 
29         #region 用於在數據庫中存儲圖片的路徑,因絕對路徑不能用。所以使用自己定義的相對路徑。
30         string filepath_2 = "~/img/";
31 
32         string filefullname_2 = filepath_2 + inputfile.FileName;
33         #endregion
34 
35         string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
36 
37         using (SqlConnection con = new SqlConnection(strcon))
38         {
39             if (con.State == ConnectionState.Closed)
40             {
41                 con.Open();
42             }
43        //變量filefullname_2存放的是自定義的虛擬路徑。
44 string sql = "INSERT INTO photo (id, mark, pathfile) VALUES (@id, @mark, @filefullname_2)";
45 46 SqlParameter[] para = new SqlParameter[] 47 { 48 new SqlParameter("@id", this.txt_id.Text), 49 new SqlParameter("@mark", this.txt_mark.Text), 50 new SqlParameter("@filefullname_2", filefullname_2) 51 }; 52 53 SqlCommand cmd = new SqlCommand(sql, con); 54 55 cmd.Parameters.AddRange(para); 56 57 if (Convert.ToInt32(cmd.ExecuteNonQuery()) > 0) 58 { 59 this.Label1.Text = "添加成功!"; 60 this.txt_id.Text = ""; 61 this.txt_mark.Text = ""; 62 } 63 else 64 { 65 this.Label1.Text = "添加失敗!"; 66 } 67 } 68 } 69 }

*解說上面的代碼:

其中后台里面涉及到把本地磁盤上的圖片,傳到服務器中指定的文件夾中,所以在22, 24, 26行代碼里調用系統方法,來獲取文件的絕對路徑,但是這種路徑不能直接在頁面中調用。

所以在30, 32行代碼里,又自定義了一個路徑,准確的說是用,圖片的名稱,加上前面自定義的相對路徑。這樣,在上傳本地文件到服務器的時候,就用系統的那個路徑,在界面調用的時候就用,自定義的那個路徑。(在數據庫中存儲的就是自定義的路徑)。

至此,向服務器中上傳圖片的代碼就寫好了。其中還用到一些ADO.NET和參數數組(防止SQL注入)的知識,整體來說就是這樣,還不是很完善,在以后的使用中,看缺少什么功能,在加上就行了。

 

 

 

下面是第二個網頁文件中的頁面代碼:

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="select_photo.aspx.cs" Inherits="select_photo" Debug ="true"%>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head id="Head1" runat="server">
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12     <div>
13         要查看的圖片ID:<asp:TextBox ID="txt_id" runat="server"></asp:TextBox>
14         <asp:Button ID="Button1" runat="server" Text="查看" OnClick ="Button1_Click"/><br />
15         描述:<asp:Label ID="Label1" runat="server"></asp:Label><br />
16         <asp:Image ID="Image1" runat="server" OnLoad ="Image1_Load"/>
17     </div>
18     </form>
19 </body>
20 </html>

 

這是二個網頁文件的后台代碼:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 using System.Data;
 9 using System.Configuration;
10 using System.Data.SqlClient;
11 using System.IO;
12 
13 
14 public partial class select_photo : System.Web.UI.Page
15 {
16     protected void Page_Load(object sender, EventArgs e)
17     {
18       //其實是不需要使用的
19     }
20     protected void Image1_Load(object sender, EventArgs e)
21     {
        //這個其實也是不需要的,因為要輸入ID去取的數據庫中的圖片路徑,所以不能再初始化的時候就調用代碼。
22 } 23 24 protected void Button1_Click(object sender, EventArgs e) 25 { 26 string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString; 27 28 using (SqlConnection con = new SqlConnection(strcon)) 29 { 30 if (con.State == ConnectionState.Closed) 31 { 32 con.Open(); 33 } 34 35 try 36 { 37 string sql = "SELECT mark, pathfile FROM photo WHERE ID = @ID"; 38 39 SqlParameter[] para = new SqlParameter[] 40 { 41 new SqlParameter("@ID", this.txt_id.Text) 42 }; 43 44 SqlCommand cmd = new SqlCommand(sql, con); 45 46 cmd.Parameters.AddRange(para); 47 48 //SqlDataReader sdr = cmd.ExecuteReader(); 49 50 //sdr.Read(); 51 52 //string pathfile = sdr[0].ToString(); 53 54 //string mark = sdr[1].ToString(); 55 56 SqlDataReader dap = cmd.ExecuteReader(); 57 58 DataTable dt = new DataTable(); 59 60 dt.Load(dap); 61 62 string mark = dt.Rows[0][0].ToString(); 63 //解析:由select語句,查詢得到的是一行結果。 64 //那么dt.Rows[0][0].ToString();代表的就是第一行的第一列。對應的就是查詢出的pathfile字段的值。 65 //dt.Rows[0][1].ToString();代表的就是第一行的第二列。對應查詢出的就是mark字段的值。 66 67 string pathfile = dt.Rows[0][1].ToString(); 68 69 Image1.ImageUrl = pathfile; 70 71 Label1.Text = mark; 72 } 73 catch 74 { 75 Response.Write("輸入的數據不正確!"); 76 } 77 } 78 } 79 }

*解說:

其中后台代碼中,注釋的代碼,是可以用另一種方法去實現,我這里使用的是datatable 也可是使用SqlDataReader。

上面代碼的前台沒什么好說的,最關鍵是后台,后台代碼中的和核心代碼是這句:Image1.ImageUrl = pathfile;以及,如何使用datatable或者SqlDataReader。從數據庫中取的數據,然后根據數據在表中的第幾行第幾列進行給相應的空間進行賦值。

 

這就是圖片的上傳以及顯示,加油啊。問題肯定會出現,關鍵就是以平和的平和的心態去搜集資料去解決它。不要放棄,既然你都覺的這么難,那么別人坑定也會感覺到難,所以,到現在為止,就看誰更有耐性了,加油,騷年!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM