仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 630|回复: 9
打印 上一主题 下一主题

[学习教程] ASP网页编程之User Tips: Using Return Values from ...

[复制链接]
只想知道 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:59:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

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

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-9-29 11:12

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表