花了一下午时间,终于学会了在数据库中存储图片,以及在界面中显示图片了。

存储图片有两种方法:

一种是:直接把图片转换成二进制文件存储在数据库中。

一种是:存储图片的路径到数据库,用的时候直接调用路径给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

4

5

6

7

8

9

10

11

12

13 图片ID:  

14 图片描述:  

15   

16   

17

18

19

20

21

这是第一个网页文件的后台代码:

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

4

5

6

7

8

9

10

11

12

13 要查看的图片ID:

14

15 描述:

16

17

18

19

20

这是二个网页文件的后台代码:

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。从数据库中取的数据,然后根据数据在表中的第几行第几列进行给相应的空间进行赋值。

这就是图片的上传以及显示,加油啊。问题肯定会出现,关键就是以平和的平和的心态去搜集资料去解决它。不要放弃,既然你都觉的这么难,那么别人坑定也会感觉到难,所以,到现在为止,就看谁更有耐性了,加油,骚年!