c#winform读取oracle中blob字段的图片并且显示到pictureBox里保存进库

上传人:平*** 文档编号:11091189 上传时间:2017-10-11 格式:DOC 页数:6 大小:118.45KB
返回 下载 相关 举报
c#winform读取oracle中blob字段的图片并且显示到pictureBox里保存进库_第1页
第1页 / 共6页
c#winform读取oracle中blob字段的图片并且显示到pictureBox里保存进库_第2页
第2页 / 共6页
c#winform读取oracle中blob字段的图片并且显示到pictureBox里保存进库_第3页
第3页 / 共6页
c#winform读取oracle中blob字段的图片并且显示到pictureBox里保存进库_第4页
第4页 / 共6页
c#winform读取oracle中blob字段的图片并且显示到pictureBox里保存进库_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《c#winform读取oracle中blob字段的图片并且显示到pictureBox里保存进库》由会员分享,可在线阅读,更多相关《c#winform读取oracle中blob字段的图片并且显示到pictureBox里保存进库(6页珍藏版)》请在金锄头文库上搜索。

1、1. private void button2_Click(object sender, EventArgs e) 2. 3. OracleConnection conn = dbc.getConnection();/获得 conn连接 4. try 5. 6. conn.Open(); 7. OracleCommand cmd = conn.CreateCommand(); 8. cmd.CommandText = SELECT zp FROM kk.kkbj WHERE xh = 2345 ;/查询获得图片流 9. 10. OracleDataReader reader = cmd.Exe

2、cuteReader();/创建一个 OracleDateReader对象 11. reader.Read(); 12. 13. MemoryStream ms = new MemoryStream(byte)readerzp);14. 15. Image image = Image.FromStream(ms, true); 16. 17. reader.Close(); 18. conn.Close(); 19. 20. pictureBox1.Image = image; 21. 22. catch (Exception ee) 23. 24. MessageBox.Show(ee.Me

3、ssage.ToString(); 25. 26. 27. -下边是上传和存入数据库有 和 winform(转)- 本文总结如何在.Net Winform 和.Net webform()中将图片存入sqlserver中并读取显示的方法 1,使用 将图片上传并存入 SqlServer中,然后从 SqlServer中读取并显示出来 一,上传并存入 SqlServer 数据库结构 create table test id identity(1,1), FImage image 相关的存储过程 Create proc UpdateImage ( UpdateImage Image ) As Inser

4、t Into test(FImage) values(UpdateImage) GO 在 UpPhoto.aspx文件中添加如下: C#代码 1. 2. 然后在后置代码文件 UpPhoto.aspx.cs添加 btnAdd按钮的单击事件处理代码: C#代码 1. private void btnAdd_Click(object sender, System.EventArgs e) 2. 3. /获得图象并把图象转换为 byte 4. HttpPostedFile upPhoto=UpPhoto.PostedFile; 5. int upPhotoLength=upPhoto.ContentL

5、ength; 6. byte PhotoArray=new ByteupPhotoLength; 7. Stream PhotoStream=upPhoto.InputStream; 8. PhotoStream.Read(PhotoArray,0,upPhotoLength); 9. 10. /连接数据库 11. SqlConnection conn=new SqlConnection(); 12. conn.ConnectionString=Data Source=localhost;Database=test;User Id=sa;Pwd=sa; 13. 14. SqlCommand c

6、md=new SqlCommand(UpdateImage,conn); 15. cmd.CommandType=CommandType.StoredProcedure; 16. 17. cmd.Parameters.Add(UpdateImage,SqlDbType.Image); 18. cmd.ParametersUpdateImage.Value=PhotoArray; 19. 20. /如果你希望不使用存储过程来添加图片把上面四句代码改为:21. /string strSql=Insert into test(FImage) values(FImage); 22. /SqlComma

7、nd cmd=new SqlCommand(strSql,conn); 23. /cmd.Parameters.Add(FImage,SqlDbType.Image); 24. /cmd.ParametersFImage.Value=PhotoArray; 25. 26.conn.Open(); 27.cmd.ExecuteNonQuery(); 28.conn.Close(); 29. 二,从 SqlServer中读取并显示出来 在需要显示图片的地方添加如下代码: C#代码 1. ShowPhoto.aspx主体代码: C#代码 1. private void Page_Load(objec

8、t sender, System.EventArgs e) 2. 3. if(!Page.IsPostBack) 4. 5. SqlConnection conn=new SqlConnection() 6. conn.ConnectionString=Data Source=localhost;Database=test;User Id=sa;Pwd=sa; 7. 8. string strSql=select * from test where id=2;/这里假设获取 id为 2的图片 9. SqlCommand cmd=new SqlCommand() 10. reader.Read(

9、); 11. Response.ContentType=application/octet-stream; 12. Response.BinaryWrite(Byte)readerFImage); 13. Response.End(); 14. reader.Close(); 15. 16. 3,在 winform中将图片存入 sqlserver,并从 sqlserver中读取并显示在picturebox中 1,存入 sqlserver 数据库结构和使用的存储过过程,同上面的一样 1.1,在窗体中加一个 OpenFileDialog控件,命名为 ofdSelectPic 1.2,在窗体上添加一

10、个打开文件按钮,添加如下单击事件代码: C#代码 1. Stream ms; 2. byte picbyte; 3. /ofdSelectPic.ShowDialog(); 4. if (ofdSelectPic.ShowDialog()=DialogResult.OK) 5. 6. if (ms=ofdSelectPic.OpenFile()!=null) 7. 8. /MessageBox.Show(ok); 9. picbyte=new bytems.Length; 10. ms.Position=0; 11. ms.Read(picbyte,0,Convert.ToInt32(ms.L

11、ength); 12. /MessageBox.Show(读取完毕!); 13. 14. /连接数据库 15. SqlConnection conn=new SqlConnection(); 16. conn.ConnectionString=Data Source=localhost;Database=test;User Id=sa;Pwd=sa; 17. 18. SqlCommand cmd=new SqlCommand(UpdateImage,conn); 19. cmd.CommandType=CommandType.StoredProcedure; 20. 21. cmd.Param

12、eters.Add(UpdateImage,SqlDbType.Image); 22. cmd.ParametersUpdateImage.Value=picbyte; 23. 24. conn.Open(); 25. cmd.ExecuteNonQuery(); 26. conn.Close(); 27. 28. ms.Close(); 29. 30. 2,读取并显示在 picturebox中 2.1 添加一个 picturebox,名为 ptbShow 2.2 添加一个按钮,添加如下响应事件: C#代码 1. SqlConnection conn=new SqlConnection();

13、2. conn.ConnectionString=Data Source=localhost;Database=test;UserId=sa;Pwd=sa; 3. 4. string strSql=select FImage from test where id=1; 5. 6. SqlCommand cmd=new SqlCommand(strSql,conn); 7. 8. conn.Open(); 9. SqlDataReader reader=cmd.ExecuteReader(); 10. reader.Read(); 11. 12.MemoryStream ms=new MemoryStream(byte)readerFImage); 13. 14.Image image=Image.FromStream(ms,true); 15. 16. reader.Close(); 17. conn.Close(); 18. 19.ptbShow.Image=image;

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

当前位置:首页 > 行业资料 > 其它行业文档

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