如何在SQLServer中保存和输出图片
2008-04-26 09:27
295 查看
建表 为了试验这个例子你需要一个含有数据的table(你可以在现在的库中创建它,也可以创建一个新的),下面是它的结构:
ColumnName Datatype Purpose ID Integer identitycolumnPrimarykey IMGTITLE Varchar(50) Storessomeuserfriendlytitletoidentitytheimage IMGTYPE Varchar(50) Storesimagecontenttype.Thiswillbesameasrecognizedcontenttypesof IMGDATA Image Storesactualimageorbinarydata.保存images进数据库 为了保存图片到table你首先得从客户端上传它们到你的web服务器。你可以创建一个webform,用TextBox得到图片的标题,用FileServerControl得到图片文件。确信你设定了Form的encType属性为multipart/form-data。
Streamimgdatastream=File1.PostedFile.InputStream; intimgdatalen=File1.PostedFile.ContentLength; stringimgtype=File1.PostedFile.ContentType; stringimgtitle=TextBox1.Text; byte[]imgdata=newbyte[imgdatalen]; intn=imgdatastream.Read(imgdata,0,imgdatalen); stringconnstr= ((NameValueCollection)Context.GetConfig ("appSettings"))["connstr"]; SqlConnectionconnection=newSqlConnection(connstr); SqlCommandcommand=newSqlCommand ("INSERTINTOImageStore(imgtitle,imgtype,imgdata) VALUES(@imgtitle,@imgtype,@imgdata)",connection); SqlParameterparamTitle=newSqlParameter ("@imgtitle",SqlDbType.VarChar,50); paramTitle.Value=imgtitle; command.Parameters.Add(paramTitle); SqlParameterparamData=newSqlParameter ("@imgdata",SqlDbType.Image); paramData.Value=imgdata; command.Parameters.Add(paramData); SqlParameterparamType=newSqlParameter ("@imgtype",SqlDbType.VarChar,50); paramType.Value=imgtype; command.Parameters.Add(paramType); connection.Open(); intnumRowsAffected=command.ExecuteNonQuery(); connection.Close();
privatevoidPage_Load(objectsender,System.EventArgse) { stringimgid=Request.QueryString["imgid"]; stringconnstr=((NameValueCollection) Context.GetConfig("appSettings"))["connstr"]; stringsql="SELECTimgdata,imgtypeFROMImageStoreWHEREid=" +imgid; SqlConnectionconnection=newSqlConnection(connstr); SqlCommandcommand=newSqlCommand(sql,connection); connection.Open(); SqlDataReaderdr=command.ExecuteReader(); if(dr.Read()) { Response.ContentType=dr["imgtype"].ToString(); Response.BinaryWrite((byte[])dr["imgdata"]); } connection.Close(); }
相关文章推荐
- 在SQLServer中保存和输出图片
- 如何将图片保存到SqlServer、Oracle、Access数据库中。
- 如何将图片保存到SqlServer、Oracle、Access数据库中
- Matlab中如何将视频转换成一帧一帧的图片并输出保存
- 如何截屏然后保存到图片文件
- Java如何保存网络上的图片
- MATLAB如何输出无白边图片的操作方法
- Java生成直接页面输出二维码,但不保存二维码图片
- 如何把Excel中的单元格等对象保存成图片
- 如何将图片保存到sql数据库
- 如何将图片保存至自定义分组
- R语言——在linux环境下如何画图或保存图片
- cocos2d-x如何截屏并保存图片
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 数组保存图片地址然后输出到网页为什么只显示最后一张
- 如何利用php+mysql保存和输出文件【转换成二进制】
- [转载]如何将cmd中命令输出保存
- 将控件中的数据输出保存到本地excel或word中,同时保存图片到本地(c#)
- simulink中如何将scope图像导出保存成图片格式
- 在SQL Server中保存和输出图片