代码不是很完整
1 /// <summary>
2 /// 将图片存储到数据库中
3 /// </summary>
4 /// <param name="filePath">文件路径</param>
5 public static void SaveImageToDB(string filePath)
6 {
7 FileStream fileStream = new FileStream(filePath, FileMode.Open);
8 byte[] file = new byte[fileStream.Length];
9 fileStream.Read(file, 0, (int)fileStream.Length);
10 fileStream.Close();
11
12
13 string sqlInsert = "insert into Img (id,Img) values ('0001',@Img)";
14 SqlConnection conn = new SqlConnection("server=127.0.0.1;database=Test;uid=sa;pwd=sa;");
15 conn.Open();
16 SqlCommand cmd = new SqlCommand(sqlInsert, conn);
17 cmd.CommandType = CommandType.Text;
18
19 cmd.Parameters.Add(new SqlParameter("@Img", SqlDbType.Image, file.Length,
20 ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, file));
21
22 cmd.ExecuteNonQuery();
23 conn.Close();
24 }
25
26
27 /// <summary>
28 /// 从数据库中读取图片数据,并用该数据创建新图片
29 /// </summary>
30 /// <param name="filePath">文件路径</param>
31 public static void CreateImageFromDB(string filePath)
32 {
33 byte[] file ;
34 string sqlSelect = "select top 1 Img from Img";
35 SqlConnection conn = new SqlConnection("server=localhost;database=Test;uid=sa;pwd=sa;");
36 conn.Open();
37 SqlCommand cmd = new SqlCommand(sqlSelect, conn);
38 SqlDataReader dr = cmd.ExecuteReader();
39
40 if (!dr.HasRows)
41 {
42 dr.Close();
43 conn.Close();
44 return;
45 }
46
47 dr.Read();
48
49 if(dr["Img"] == DBNull.Value)
50 {
51 dr.Close();
52 conn.Close();
53 return;
54 }
55
56 file = new byte[Convert.ToInt32(dr.GetBytes(0, 0, null, 0, Int32.MaxValue))];
57 dr.GetBytes(0, 0, file, 0, file.Length);
58
59 dr.Close();
60 conn.Close();
61
62
63 WriteFile(filePath, file);
64 }
2 /// 将图片存储到数据库中
3 /// </summary>
4 /// <param name="filePath">文件路径</param>
5 public static void SaveImageToDB(string filePath)
6 {
7 FileStream fileStream = new FileStream(filePath, FileMode.Open);
8 byte[] file = new byte[fileStream.Length];
9 fileStream.Read(file, 0, (int)fileStream.Length);
10 fileStream.Close();
11
12
13 string sqlInsert = "insert into Img (id,Img) values ('0001',@Img)";
14 SqlConnection conn = new SqlConnection("server=127.0.0.1;database=Test;uid=sa;pwd=sa;");
15 conn.Open();
16 SqlCommand cmd = new SqlCommand(sqlInsert, conn);
17 cmd.CommandType = CommandType.Text;
18
19 cmd.Parameters.Add(new SqlParameter("@Img", SqlDbType.Image, file.Length,
20 ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, file));
21
22 cmd.ExecuteNonQuery();
23 conn.Close();
24 }
25
26
27 /// <summary>
28 /// 从数据库中读取图片数据,并用该数据创建新图片
29 /// </summary>
30 /// <param name="filePath">文件路径</param>
31 public static void CreateImageFromDB(string filePath)
32 {
33 byte[] file ;
34 string sqlSelect = "select top 1 Img from Img";
35 SqlConnection conn = new SqlConnection("server=localhost;database=Test;uid=sa;pwd=sa;");
36 conn.Open();
37 SqlCommand cmd = new SqlCommand(sqlSelect, conn);
38 SqlDataReader dr = cmd.ExecuteReader();
39
40 if (!dr.HasRows)
41 {
42 dr.Close();
43 conn.Close();
44 return;
45 }
46
47 dr.Read();
48
49 if(dr["Img"] == DBNull.Value)
50 {
51 dr.Close();
52 conn.Close();
53 return;
54 }
55
56 file = new byte[Convert.ToInt32(dr.GetBytes(0, 0, null, 0, Int32.MaxValue))];
57 dr.GetBytes(0, 0, file, 0, file.Length);
58
59 dr.Close();
60 conn.Close();
61
62
63 WriteFile(filePath, file);
64 }
浙公网安备 33010602011771号