C#(WinForm)上传图片保存到数据库和从数据库读取图片显示到窗体

  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         //上传保存到数据库

 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 从数据库读取图片显示到窗体:

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                     //通过内存流MemoryStream,

226  

227                     //把byte[]数组fileContent加载到Image中并赋值给图片框的Image属性,

228  

229                     //让数据库中的图片直接显示在窗体上。

230  

231                     MemoryStream ms = new MemoryStream(bytFile, 0, bytFile.Length);

232  

233                     this.picImage.Image = Image.FromStream(ms);

234  

235                     //关闭内存流

236  

237                     ms.Close();

238  

239                 }

240  

241                 catch

242  

243                 {

244  

245                     conn.Close();

246  

247                     MessageBox.Show("失败");

248  

249                 }

250  

251         }

代码转自IT学习广场http://www.itxxgc.com/net/detail/30

                 来自凌波小屋-----冯和超的笔记-------

你可能感兴趣的:(WinForm)