仓酷云

标题: ASP网页编程之User Tips: Using Return Values from ... [打印本页]

作者: 只想知道    时间: 2015-1-16 22:59
标题: ASP网页编程之User Tips: Using Return Values from ...
对于中小型web应用来说,php有很强的竞争力,linux+apache+mysql+php(lamp)的组合几乎可以胜任绝大多数网站的解决方案,对于大型应用来讲,对于系统架构要求更高,需要有成熟的框架支持,jsp的struts是个不错的框架,国内介绍它的资料也非常多,应用逐渐广泛起来。asp就不用说了,error|serverWhenIstarteddevelopingwebpagesthatinteractwithaSQLServerdatabase,Iprobablystartedlikeeverbodyelse:withinlineSQLstatements.Ithenprogressedtousingtheconnectionobjecttocallstoredproceduresandeventuallystartedusingthecommandobject.Ieventuallyrealizedhowusefulreturnvaluesfromstoredprocedurescouldbe,sinceIcouldusethemtoreturnavaluebasedonapotentialerrorconditionthatIcheckforinthestoredprocedure.

Recently,Iwasdevelopinganonlinecatalogandhadasituationtodealwith:


Userentersdataintoaform

Needtovalidatetheentries(easyenoughwithclient-sidejavascript)

NeedtoinsertthedataintoaSQLServerdatabaseaftercheckingtomakesurevariousconditionsdontexist.Forexample,theusercouldenteraproduct,butonlyiftheproductdoesntalreadyexistinthecatalog.Thatsnotsomethingthatseasilyaccomplishedwithclient-sidevalidation!
InitiallyIdecideduponafairlypopularroute:createaforminPage1.aspthatsubmitstoPage2.aspwhichattemptstoinserttheuser-enteredinformationintothedatabase.Iftheproductalreadyexists,gobacktoPage1.asp,displayingamessageandpopulatingthefieldswithwhattheuserentered.Whilethisisapossibleapproach,trustmewhenIsaythatitsapaintocodeifyouhavealotofformfields!IdeallyIwantedapop-upmessagethatIcouldcustomizebasedontheconditionfoundinthestoredprocedure.(Ilikepop-upsbecausebytheirnature,theydrawmoreattentionthanamessagedisplayedonapage.)Also,IwantedtheusertakenbacktoPage1.aspwithallofhis/herentriesalreadyfilledin.

Hereisanexamplestoredprocedurethatreturnsanerrorresultifsomethinggoesawry:

CreateProcedure[Proc_InsertProduct]
(
@productnamevarchar(50)=null,
@pricemoney=null
}
AS

ifexists(selectproductnamefromtblProductswhereproductname=@productname)
return55555
else
insertintotblproducts(productname,price)
values(@productname,@price)

return@@error




Asimplesamplestoredprocedurethatcheckstoseeiftheproductalreadyexists.Ifso,itreturns55555,otherwiseitinsertstheproductandreturnstheerrorcode(whichwill0ifsuccessful).Now,ontheASPside,Iusethecommandobjecttosendtheformcontentstothestoredprocedure.Thestoredproceduresreturnvalueisalwaysthefirstitemintheparameterscollectionofthecommandobject(cmd.parameters(0))afterthecommandobjectsExecutemethodhasbeencalledinthiscase.

<%
optionexplicit
response.buffer=true
response.exires=-1441

dimconnect,cmd,returnvalue

setconnect=server.createobject("adodb.connection")
setcmd=server.createobject("adodb.command")
connect.openYourConnectionString
setcmd.activeconnection=connect
cmd.commandtype=4sp(Iknow,magicnumbers,butaddacommentandthereyougo)
cmd.commandtext="Proc_InsertProduct"
cmd.parameters(1)=request.form("productname")
cmd.parameters(2)=request.form("price")
cmd.execute
%>




Atthispointthestoredprocedurehasbeenexecutedandcmd.parameters(0)containsthereturnvalue.Now,ifthereturnvalueisnot0,meaningsomesortoferroroccurred,wewishtocalltheMyErrorsubroutine,whichwillgenerateaJavaScriptpopuperrormessage.Otherwise,ifthereisnoerror,sendtheuserontosomeotherpage,onethat,perhaps,displaysaconfirmationmessageofthedatabaseactionjustperformed.

<%
returnvalue=cmd.parameters(0)

Didanerroroccur?
ifreturnvalue0
Somesortoferroroccurred
response.writeMyError(returnvalue)
else
Noerrors...
response.redirect("Whereever.asp")
endif

setcmd=nothing
connect.close
setconnect=nothing
%gt;




TheMyErrorsubroutinenowneedstousesomeclient-sideJavascriptcodetosendtheuserbackapop-upmessage;oncethispopupmessageisreadandtheOKbuttonisclicked,itshouldtaketheuserbacktoPage1.asp.

<%
functionMyError(errorcode)
dimtitle,message

selectcaseerrorcode
&nbs</p>缺乏可以共同遵循的行业标准,ASP还处在发展初期,大家对它的理解不同,如产品和服务标准,收费标准等,不利于行业的健康发展。
作者: 只想知道    时间: 2015-1-18 12:34
用户端的浏览器不需要提供任何别的支持,这样大提高了用户与服务器之间的交互的速度。
作者: 仓酷云    时间: 2015-1-21 23:36
学习ASP其实应该上升到如何学习程序设计这种境界,其实学习程序设计又是接受一种编程思想。比如ASP如何学习,你也许在以前的学习中碰到过。以下我仔细给你说几点:
作者: 再见西城    时间: 2015-1-28 18:23
ASP(ActiveServerPages)是Microsfot公司1996年11月推出的WEB应用程序开发技术,它既不是一种程序语言,也不是一种开发工具,而是一种技术框架,不须使用微软的产品就能编写它的代码,能产生和执行动态、交互式、高效率的站占服务器的应用程序。
作者: 小女巫    时间: 2015-2-5 15:29
ASP.Net摆脱了以前ASP使用脚本语言来编程的缺点,理论上可以使用任何编程语言包括C++,VB,JS等等,当然,最合适的编程语言还是MS为.NetFrmaework专门推出的C(读csharp),它可以看作是VC和Java的混合体吧。
作者: 柔情似水    时间: 2015-2-12 17:15
先学习用frontpage熟悉html编辑然后学习asp和vbscript建议买书进行系统学习
作者: 谁可相欹    时间: 2015-3-3 03:51
我可以结合自己的经验大致给你说一说,希望对你有所帮助,少走些弯路。
作者: 深爱那片海    时间: 2015-3-11 09:15
封装性使得代码逻辑清晰,易于管理,并且应用到ASP.Net上就可以使业务逻辑和Html页面分离,这样无论页面原型如何改变,业务逻辑代码都不必做任何改动;继承性和多态性使得代码的可重用性大大提高。
作者: 分手快乐    时间: 2015-3-18 02:52
如何学好ASP,以前也有人问过,把回答给你转过来看看能否对你有帮助:
作者: 飘灵儿    时间: 2015-3-25 09:59
我可以结合自己的经验大致给你说一说,希望对你有所帮助,少走些弯路。




欢迎光临 仓酷云 (http://ckuyun.com/) Powered by Discuz! X3.2