存储和检索图像reg [英] storing and retriving images reg
本文介绍了存储和检索图像reg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将图像存储在数据库中,并使用c#通过asp.net检索它.
我正在使用sql 2005数据库,文件上传器和存储过程.
请帮我解决这个问题.
在此先感谢.
I need to store the images in database and retrive it through asp.net with c#.
I am using sql 2005 database,file uploader and stored procedures.
Please help me to solve this.
Thanks in Advance.
推荐答案
要将图像存储在Sql Server中,请使用以下代码:
To store Image in Sql server use following code:
Stream imgStream = FileUpload1.PostedFile.InputStream;
BinaryReader imgBinary = new BinaryReader(imgStream);
Byte[] bytes = imgBinary.ReadBytes((Int32)imgStream.Length);
imgBinary.Close();
imgStream.Close();
string imageName = FileUpload1.PostedFile.FileName;
string imgBinary = Convert.ToBase64String(bytes);
//Store the imageName and imgBinary in the database. Make sure the column data type for imgBinary is Image.
现在要从数据库中检索并显示存储的图像,请创建新的aspx页面,并将以下代码放在Page_Load事件中.
Now to retrieve and show the stored image from database, create new aspx page and put below code in Page_Load event.
string imgName = Request.QueryString["ImageName"].ToString();
string sql = "Select Img from TestImage where ImgName=''" + imgName + "''";
SqlCommand myCommand = new SqlCommand(sql, con);
con.Open();
object objImage = myCommand.ExecuteScalar();
Response.Clear();
Response.ContentType = "image/jpeg";
Response.BinaryWrite((byte[])objImage);
Response.End();
选中此图片处理在ASP.NET中 [ ^ ]
这篇关于存储和检索图像reg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文