在SQLServer中保存和输出图片[MSSQL防范]
本文“在SQLServer中保存和输出图片[MSSQL防范]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
在sql server中保存和输出图片
cashcho(翻译)
关键字 SQL Server images
出处 http://www.bipinjoshi.com/
介绍
有时刻我们需求保存一些binary data进数据库.SQL Server供应一个叫做image的特别数据范例供我们保存binary data.Binary data可以是图片、文档等.在这篇文章中我们将看到如安在SQL Server中保存和输出图片.
建表
为了试验这个例子你需求一个含有数据的table(你可以在目前的库中成立它,也可以成立一个新的数据库),下面是它的构造:
Column Name | Datatype | Purpose |
ID | Integer | identity column Primary key |
IMGTITLE | Varchar(50) | Stores some user friendly title to identity the image |
IMGTYPE | Varchar(50) | Stores image content type. This will be same as recognized content types of ASP.NET |
IMGDATA |
Image | Stores actual image or binary data. |
保存images进SQL Server数据库
为了保存图片到table你首先得从客户端上传它们到你的Web服务器.你可以成立一个web form,用TextBox得到图片的标题,用HTML File Server Control得到图片文件.确信你设定了Form的encType属性为multipart/form-data.
Stream imgdatastream = File1.PostedFile.InputStream;www.c hinaitpower.comlOPhU7 int imgdatalen = File1.PostedFile.ContentLength;www.c hinaitpower.comlOPhU7 string imgtype = File1.PostedFile.ContentType;www.c hinaitpower.comlOPhU7 string imgtitle = TextBox1.Text;www.c hinaitpower.comlOPhU7 byte[] imgdata = new byte[imgdatalen];www.c hinaitpower.comlOPhU7 int n = imgdatastream.Read(imgdata,0,imgdatalen);www.c hinaitpower.comlOPhU7 string connstr=www.c hinaitpower.comlOPhU7 ((NameValueCollection)Context.GetConfigwww.c hinaitpower.comlOPhU7 ("appSettings"))["connstr"];www.c hinaitpower.comlOPhU7 SqlConnection connection = new SqlConnection(connstr);www.c hinaitpower.comlOPhU7 SqlCommand command = new SqlCommandwww.c hinaitpower.comlOPhU7 ("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)www.c hinaitpower.comlOPhU7 VALUES ( @imgtitle, @imgtype,@imgdata )", connection );www.c hinaitpower.comlOPhU7 www.c hinaitpower.comlOPhU7 SqlParameter paramTitle = new SqlParameterwww.c hinaitpower.comlOPhU7 ("@imgtitle", SqlDbType.VarChar,50 );www.c hinaitpower.comlOPhU7 paramTitle.Value = imgtitle;www.c hinaitpower.comlOPhU7 command.Parameters.Add( paramTitle);www.c hinaitpower.comlOPhU7 www.c hinaitpower.comlOPhU7 SqlParameter paramData = new SqlParameterwww.c hinaitpower.comlOPhU7 ( "@imgdata", SqlDbType.Image );www.c hinaitpower.comlOPhU7 paramData.Value = imgdata;www.c hinaitpower.comlOPhU7 command.Parameters.Add( paramData );www.c hinaitpower.comlOPhU7 www.c hinaitpower.comlOPhU7 SqlParameter paramType = new SqlParameterwww.c hinaitpower.comlOPhU7 ( "@imgtype", SqlDbType.VarChar,50 );www.c hinaitpower.comlOPhU7 paramType.Value = imgtype;www.c hinaitpower.comlOPhU7 command.Parameters.Add( paramType );www.c hinaitpower.comlOPhU7 www.c hinaitpower.comlOPhU7 connection.Open();www.c hinaitpower.comlOPhU7 int numRowsAffected = command.ExecuteNonQuery();www.c hinaitpower.comlOPhU7 connection.Close();www.c hinaitpower.comlOPhU7 |
从数据库中输出图片
目前让我们从数据库中取出我们方才保存的图片,在这儿,我们将直接将图片输出至浏览器.你也可以将它保存为一个文件或做任何你想做的.
private void Page_Load(object sender, System.EventArgs e)www.c hinaitpower.comlOPhU7 {www.c hinaitpower.comlOPhU7 string imgid =Request.QueryString["imgid"];www.c hinaitpower.comlOPhU7 string connstr=((NameValueCollection)www.c hinaitpower.comlOPhU7 Context.GetConfig("appSettings"))["connstr"];www.c hinaitpower.comlOPhU7 string sql="SELECT imgdata, imgtype FROM ImageStore WHERE id = "www.c hinaitpower.comlOPhU7 + imgid;www.c hinaitpower.comlOPhU7 SqlConnection connection = new SqlConnection(connstr);www.c hinaitpower.comlOPhU7 SqlCommand command = new SqlCommand(sql, connection);www.c hinaitpower.comlOPhU7 connection.Open();www.c hinaitpower.comlOPhU7 SqlDataReader dr = command.ExecuteReader();www.c hinaitpower.comlOPhU7 if(dr.Read())www.c hinaitpower.comlOPhU7 {www.c hinaitpower.comlOPhU7 Response.ContentType = dr["imgtype"].ToString();www.c hinaitpower.comlOPhU7 Response.BinaryWrite( (byte[]) dr["imgdata"] );www.c hinaitpower.comlOPhU7 }www.c hinaitpower.comlOPhU7 connection.Close();www.c hinaitpower.comlOPhU7 }www.c hinaitpower.comlOPhU7 |
在上面的代码中我们利用了一个已经翻开的数据库,通过datareader挑选images.接着用Response.BinaryWrite替换Response.Write来显示image文件.
但愿您喜好这些文章,若有任何看法和倡议请致信webmaster@bipinjoshi.com.
本文转载自中国软件(http://www.csdn.net). 以上是“在SQLServer中保存和输出图片[MSSQL防范]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |