C图片保存到数据库和从数据库读取图片并显示

上传人:hs****ma 文档编号:557872096 上传时间:2023-07-22 格式:DOC 页数:2 大小:18.50KB
返回 下载 相关 举报
C图片保存到数据库和从数据库读取图片并显示_第1页
第1页 / 共2页
C图片保存到数据库和从数据库读取图片并显示_第2页
第2页 / 共2页
亲,该文档总共2页,全部预览完了,如果喜欢就下载吧!
资源描述

《C图片保存到数据库和从数据库读取图片并显示》由会员分享,可在线阅读,更多相关《C图片保存到数据库和从数据库读取图片并显示(2页珍藏版)》请在金锄头文库上搜索。

1、图片保存到数据库的方法:publicvoidimgToDB(stringsql)参数sql中要求保存的imge变量名称为images调用方法如:imgToDB(updateUserPhotosetPhoto=imageswhereUserNo=+temp+);FileStreamfs=File.OpenRead(t_photo.Text);byteimageb=newbytefs.Length;fs.Read(imageb,0,imageb.Length);fs.Close();SqlCommandcom3=newSqlCommand(sql,con);com3.Parameters.Add(

2、images,SqlDbType.Image).Value=imageb;if(com3.Connection.State=ConnectionState.Closed)com3.Connection.Open();trycom3.ExecuteNonQuery();catchfinallycom3.Connection.Close();数据库中读出图片并显示在picturebox中:方法一:privatevoidShowImage(stringsql)/调用方法如:ShowImage(selectPhotofromUserPhotowhereUserNo=+userno+);SqlComma

3、ndcmd=newSqlCommand(sql,conn);conn.Open();byteb=(byte)cmd.ExecuteScalar();if(b.Length0)MemoryStreamstream=newMemoryStream(b,true);stream.Write(b,0,b.Length);pictureBox1.Image=newBitmap(stream);stream.Close();conn.Close();方法二:当在dg中选中某行时:privatevoiddg_MouseUp(objectsender,MouseEventArgse)/整行选择if(e.But

4、ton=System.Windows.Forms.MouseButtons.Left)/用户编号,姓名,性别,身份证号,籍贯,学院,系所,校区,部门,电话照片/显示相片objectimgobj=dg10,dg.CurrentRow.Index.Value;if(imgobj!=null&!Convert.IsDBNull(imgobj)byteimgb=(byte)imgobj;MemoryStreammemStream=newMemoryStream(imgb);tryBitmapmyimge=newBitmap(memStream);this.pictureBox1.Image=myimge;catchDB.msgbox(从数据库读取相片失败!”);elsepictureBox1.Image=null;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号