资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
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.ExecuteReader();/创建一个 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.Message.ToString(); 25. 26. 27. -下边是上传和存入数据库有 asp.net和 winform(转)- 本文总结如何在.Net Winform 和.Net webform(asp.net)中将图片存入sqlserver中并读取显示的方法 1,使用 asp.net将图片上传并存入 SqlServer中,然后从 SqlServer中读取并显示出来 一,上传并存入 SqlServer 数据库结构 create table test id identity(1,1), FImage image 相关的存储过程 Create proc UpdateImage ( UpdateImage Image ) As Insert 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.ContentLength; 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 cmd=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. /SqlCommand 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(object 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(); 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,在窗体上添加一个打开文件按钮,添加如下单击事件代码: 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.Length); 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.Parameters.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(); 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;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号