Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15650
  • 博文数量: 9
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 135
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-16 16:12
文章分类
文章存档

2014年(9)

我的朋友

分类: C#/.net

2014-03-26 22:36:33

  ASP.NET数据库中图片存储及读取

  开发环境:window2000、sqlserver2000、.netframeworksdk正式版开发语言:c#、asp.net简介:数据库中图片存储及读取说明:在asp中,我们用request.totalbytes、request.binaryread()来上传图片,这个可恶的binaryread()方法非常笨,单个文件上传倒没什么大事,单如果多个图片上专可就花大气力了…!而现在asp.net中将会把解决以前asp中文件上传的种种问题,使你在asp.net中轻轻松松开发出功能强大的上传程序,下面大家看看例子啦。首先在sqlserver中建立一个图片存储的数库表,sqlscript如下:ifexists(select*fromdbo.sysobjectswhereid=object_id(n"[dbo].[image]")andobjectproperty(id,n"isusertable")=1)droptable[dbo].[image]gocreatetable[dbo].[image]([img_pk][int]identity(1,1)notnull,[img_name][varchar](50)null,[img_data][image]null,[img_contenttype][varchar](50)null)on[primary]textimage_on[primary]goaltertable[dbo].[image]withnocheckaddconstraint[pk_image]primarykeynonclustered([img_pk])on[primary]go------------------------------------------------------------

  一、上传图片:imgupload.aspx文件:选择文件看图codebehind文件:usingsystem;usingsystem.collections;usingsystem.componentmodel;usingsystem.data;usingsystem.drawing;usingsystem.web;usingsystem.web.sessionstate;usingsystem.web.ui;usingsystem.web.ui.webcontrols;usingsystem.web.ui.htmlcontrols;usingsystem.io;usingsystem.data.sqlclient;namespacestudy.uploadimage{//////imgupload的摘要说明。///publicclassimgupload:system.web.ui.page{protectedsystem.web.ui.webcontrols.buttonbutton1;protectedsystem.web.ui.htmlcontrols.htmlinputtextimgname;protectedsystem.web.ui.htmlcontrols.htmlinputfileuploadfile;privatevoidpage_load(objectsender,system.eventargse){//在此处放置用户代码以初始化页面}

  privatevoidbutton1_click(objectsender,system.eventargse){streamimgstream;intimglen;stringimgname_value;stringimgcontenttype;stringimguploadedname;imgstream=uploadfile.postedfile.inputstream;imglen=uploadfile.postedfile.contentlength;imguploadedname=uploadfile.postedfile.filename;byte[]imgbinarydata=newbyte[imglen];imgcontenttype=uploadfile.postedfile.contenttype;imgname_value=imgname.value;try{if(imgname_value.length<1){imgname_value=getlastrightof("\\",imguploadedname);}}catch(exceptionmyex){response.write(myex.message);}intn=imgstream.read(imgbinarydata,0,imglen);intnumrowsaffected=mydatabasemethod(imgname_value,imgbinarydata,imgcontenttype);if(numrowsaffected>0)response.write("uploadedimage");elseresponse.write("anerroroccurreduploadingtheimage.d");}publicstringgetlastrightof(stringlookfor,stringmystring){intstrpos;strpos=mystring.lastindexof(lookfor);returnmystring.substring(strpos+1);}publicintmydatabasemethod(stringimgname,byte[]imgbin,stringimgcontenttype){sqlconnectionconnection=newsqlconnection(application["test_conn"].tostring());stringsql="insertintoimage(img_name,img_data,img_contenttype)values(@img_name,@img_data,@img_contenttype)";sqlcommandcommand=newsqlcommand(sql,connection);sqlparameterparam0=newsqlparameter("@img_name",sqldbtype.varchar,50);param0.value=imgname;command.parameters.add(param0);sqlparameterparam1=newsqlparameter("@img_data",sqldbtype.image);param1.value=imgbin;command.parameters.add(param1);sqlparameterparam2=newsqlparameter("@img_contenttype",sqldbtype.varchar,50);param2.value=imgcontenttype;command.parameters.add(param2);connection.open();intnumrowsaffected=command.executenonquery();connection.close();returnnumrowsaffected;}#regionwebformdesignergeneratedcodeoverrideprotectedvoidoninit(eventargse){////codegen:该调用是asp.netweb窗体设计器所必需的。//initializecomponent();base.oninit(e);}//////设计器支持所需的方法-不要使用代码编辑器修改///此方法的内容。///privatevoidinitializecomponent(){this.button1.click+=newsystem.eventhandler(this.button1_click);this.load+=newsystem.eventhandler(this.page_load);}#endregion}}------------------------------------------------------------

  二、浏览图片:imgvies.aspx文件:mydsn.open();intimgid=int.parse(request.querystring["id"]);stringsqltext="selectimg_name,img_data,img_contenttypefromimagewhereimg_pk="+imgid;trace.write(sqltext);sqlcommandmycommand=newsqlcommand(sqltext,mydsn);sqldatareaderdr=mycommand.executereader();if(dr.read()){response.contenttype=(dr["img_contenttype"].tostring());response.binarywrite((byte[])dr["img_data"]);}mydsn.close();}#regionwebformdesignergeneratedcodeoverrideprotectedvoidoninit(eventargse){////codegen:该调用是asp.netweb窗体设计器所必需的。//initializecomponent();base.oninit(e);}//////设计器支持所需的方法-不要使用代码编辑器修改///此方法的内容。///privatevoidinitializecomponent(){this.load+=newsystem.eventhandler(this.page_load);}#endregion}}这样这个程序就完成了,简单吧。当然还很多改进之处,希望大家多想想多编编一定可以写出更多的图象上传程序。

阅读(1014) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~