C#(WinForm)上传图片保存到数据库和从数据库读取图片显示到窗体
生活随笔
收集整理的這篇文章主要介紹了
C#(WinForm)上传图片保存到数据库和从数据库读取图片显示到窗体
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 //瀏覽圖片
2
3 private void btnUp_Click(object sender, EventArgs e)
4
5 {
6
7 OpenFileDialog ofd = new OpenFileDialog();
8
9 ofd.Title = "選擇要上傳的圖片";
10
11 ofd.Filter = "All Files(*.*)|*.*|位圖(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg";
12
13 ofd.ShowDialog();
14
15 textBox1.Text = ofd.FileName;
16
17 if (!File.Exists(ofd.FileName))
18
19 {
20
21 MessageBox.Show("照片為空");
22
23 return;
24
25 }
26
27 }
28
29
30
31
32
33 //上傳保存到數(shù)據(jù)庫(kù)
34
35 private void btnUpLoad_Click(object sender, EventArgs e)
36
37 {
38
39 string strPath = txtbImage.Text.Trim();
40
41 FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);
42
43 byte[] byteFile = new byte[fs.Length];
44
45 fs.Read(byteFile, 0, (int)fs.Length);
46
47 fs.Close();
48
49 SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True");
50
51 try
52
53 {
54
55 SqlCommand cmd = new SqlCommand();
56
57 cmd.Connection = conn;
58
59
60
61 string strSql = "insert into test(FileName,Img) Values(@FileName,@Img)";
62
63 cmd.CommandText =strSql ;
64
65 //cmd.Parameters.AddWithValue("@FileName", strPath);
66
67 //cmd.Parameters.AddWithValue("@Img", byteFile);
68
69 //或者
70
71 SqlParameter[] parameters = new SqlParameter[2];
72
73 parameters[0] = new SqlParameter("@FileName", SqlDbType.NVarChar, 200);
74
75 parameters[0].Value = strPath;
76
77 parameters[1] = new SqlParameter("@Img", SqlDbType.Image,int.MaxValue);
78
79 parameters[1].Value = byteFile;
80
81 cmd.Parameters.AddRange(parameters);
82
83 conn.Open();
84
85 cmd.ExecuteNonQuery();
86
87 conn.Close();
88
89 MessageBox.Show("上傳成功");
90
91 }
92
93 catch
94
95 {
96
97 conn.Close();
98
99 MessageBox.Show("上傳失敗!");
100
101 }
102
103 }
104
105 從數(shù)據(jù)庫(kù)讀取圖片顯示到窗體:
106 1
107 2
108 3
109 4
110 5
111 6
112 7
113 8
114 9
115 10
116 11
117 12
118 13
119 14
120 15
121 16
122 17
123 18
124 19
125 20
126 21
127 22
128 23
129 24
130 25
131 26
132 27
133 28
134 29
135 30
136 31
137 32
138 33
139 34
140 35
141 36
142 37
143 38
144 39
145 40
146 41
147 42
148 43
149 44
150 45
151 46
152 47
153 48
154 49
155 50
156 51
157 52
158 53
159 54
160 55
161 56
162 57
163 58
164 59
165 60
166 61
167 62
168 63
169 64
170 65
171 66
172 67
173 68
174 69
175 70
176 71
177 72
178 73
179 //讀到圖片顯示到PictureBox
180
181 private void btnDownLoad_Click(object sender, EventArgs e)
182
183 {
184
185 byte[] bytFile;
186
187 SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True");
188
189 try
190
191 {
192
193 SqlCommand cmd = new SqlCommand();
194
195 string strSql = "select img from test where ID=3";
196
197 cmd.Connection = conn;
198
199 cmd.CommandText = strSql;
200
201 conn.Open();
202
203 SqlDataReader sdr = cmd.ExecuteReader();
204
205 if (sdr.Read())
206
207 {
208
209 bytFile = (Byte[])sdr["Img"];
210
211 }
212
213 else
214
215 {
216
217 bytFile = new byte[0];
218
219 }
220
221 sdr.Close();
222
223 conn.Close();
224
225 //通過內(nèi)存流MemoryStream,
226
227 //把byte[]數(shù)組fileContent加載到Image中并賦值給圖片框的Image屬性,
228
229 //讓數(shù)據(jù)庫(kù)中的圖片直接顯示在窗體上。
230
231 MemoryStream ms = new MemoryStream(bytFile, 0, bytFile.Length);
232
233 this.picImage.Image = Image.FromStream(ms);
234
235 //關(guān)閉內(nèi)存流
236
237 ms.Close();
238
239 }
240
241 catch
242
243 {
244
245 conn.Close();
246
247 MessageBox.Show("失敗");
248
249 }
250
251 }
代碼轉(zhuǎn)自IT學(xué)習(xí)廣場(chǎng)http://www.itxxgc.com/net/detail/30
? ? ? ? ? ? ? ? ?來(lái)自凌波小屋-----馮和超的筆記-------
轉(zhuǎn)載于:https://www.cnblogs.com/lingbohome/p/4655043.html
總結(jié)
以上是生活随笔為你收集整理的C#(WinForm)上传图片保存到数据库和从数据库读取图片显示到窗体的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【数据库范式】 分析题第一范式
- 下一篇: C#中Thread.IsBackgrou