|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
对于中小型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还处在发展初期,大家对它的理解不同,如产品和服务标准,收费标准等,不利于行业的健康发展。 |
|