使用.NET向SQL Server数据库存取图片

开发 后端
本文介绍了如何使用.NET向SQL Server数据库存取图片。使用ASP.NET上传、读取和显示SQL Server数据库里的图片都十分方便。

使用.NET向SQL Server数据库存取图片

使用.Net技术,我们可以很方便的将图片存入SQL Server数据库中并方便的读取显示出来,详细的实现方法我们一步一步将会了解到。先说如何将图片存储到sql server数据库中:

.NET向SQL Server数据库存取图片技巧:存入图片

使用asp.net将图片上传并存入SQL Server中,然后从SQL Server中读取并显示出来:

1)上传并存入SQL Server

数据库结构

  1. create table test   
  2. {   
  3. id identity(1,1),   
  4. FImage image   
  5. }   

相关的存储过程

  1. Create proc UpdateImage   
  2. (   
  3. @UpdateImage Image   
  4. )   
  5. As   
  6. Insert Into test(FImage) values(@UpdateImage)   
  7. GO   

在UpPhoto.aspx文件中添加如下: 

  1. < input id="UpPhoto" name="UpPhoto" runat="server" type="file">   
  2. < asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上传">< /asp:Button> 

然后在后置代码文件UpPhoto.aspx.cs添加btnAdd按钮的单击事件处理代码:

  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 Byte[upPhotoLength];   
  7. Stream PhotoStream=upPhoto.InputStream;   
  8. PhotoStream.Read(PhotoArray,0,upPhotoLength);   
  9. //连接数据库   
  10. SqlConnection conn=new SqlConnection();   
  11. conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";   
  12. SqlCommand cmd=new SqlCommand("UpdateImage",conn);   
  13. cmd.CommandType=CommandType.StoredProcedure;   
  14. cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);   
  15. cmd.Parameters["@UpdateImage"].Value=PhotoArray;   
  16. //如果你希望不使用存储过程来添加图片把上面四句代码改为:   
  17. //string strSql="Insert into test(FImage) values(@FImage)";   
  18. //SqlCommand cmd=new SqlCommand(strSql,conn);   
  19. //cmd.Parameters.Add("@FImage",SqlDbType.Image);   
  20. //cmd.Parameters["@FImage"].Value=PhotoArray;   
  21. conn.Open();   
  22. cmd.ExecuteNonQuery();   
  23. conn.Close();   
  24. }  

.NET向SQL Server数据库存取图片技巧:从SQL Server中读取并显示出来

在需要显示图片的地方添加如下代码:

  1. < asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx">< /asp:image>  

ShowPhoto.aspx主体代码:

  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. string strSql="select * from test where id=2";//这里假设获取id为2的图片   
  8. SqlCommand cmd=new SqlCommand(strSql,conn);   
  9. conn.Open();  
  10. SqlDataReader reader=cmd.ExecuteReader();  
  11. reader.Read();   
  12. Response.ContentType="application/octet-stream";   
  13. Response.BinaryWrite((Byte[])reader["FImage"]);   
  14. Response.End();   
  15. reader.Close();   
  16. }   

以上就介绍了.NET向SQL Server数据库存取图片的实现方法。

【编辑推荐】

  1. .NET二进制图片存储与读取的常见方法
  2. ASP.NET和SQL Server数据库图片存储的实现
  3. ASP.NET数据库图片存储到Sql2000中
  4. ASP.NET数据库图片上传与读取的实现
  5. ASP.NET(VB)应用之图片增加水印文字浅析
责任编辑:yangsai 来源: 网络转载
相关推荐

2011-07-22 14:22:43

java

2010-01-18 19:21:51

VB.NET存取数据库

2010-06-17 11:08:07

SQL Server

2011-08-15 15:14:54

SQL Server存储过程异常处理

2009-08-12 11:04:38

ASP.NET和SQL

2009-01-06 11:31:34

SybaseSQL Server数据库

2011-07-18 10:45:55

C#SQL Server数

2010-07-16 11:24:59

SQL Server数

2011-05-20 13:11:22

ADO.NET

2009-07-07 17:42:28

2011-08-09 09:31:39

SQL Server数connectionS

2010-07-15 17:28:50

SQL Server

2009-06-30 09:16:45

数据库存储JSP文件

2011-03-28 12:33:09

SQL Server数据库链接

2011-08-11 09:12:31

SQL Server nolock

2011-08-29 18:02:29

SQL Server FileStream

2009-08-05 09:38:28

SQL Server数

2009-06-26 14:41:48

ADO.NET

2011-04-11 14:18:44

SQL Server数图片

2011-08-15 14:29:52

SQL Server数事务
点赞
收藏

51CTO技术栈公众号