ASP网站制作之asp 中挪用存贮历程的一些例子.(2)
ASP是依赖组件的,能访问数据库的组件好多就有好多种,再有就是你微软的工具可是什么都要收钱的啊!历程WritingaStoredProcedurePartIIByNathanPond
--------------------------------------------------------------------------------
Thisarticleisacontinuationofmypreviousarticle,WritingaStoredProcedure
Letmestartoutbyfirstcorrecting(orratherupdating)somethingIsaidinmyfirstarticle.IsaidtherethatIwasntawareofawaytoupdateastoredprocedurewithoutdeletingitandrecreatingit.WellnowIam.:-)ThereisanALTERcomandyoucanuse,likethis:
ALTERPROCEDUREsp_myStoredProcedure
AS
......
Go
Thiswilloverwritethestoredprocedurethatwastherewiththenewsetofcommands,butwillkeeppermissions,soitisbetterthandroppingandrecreatingtheprocedure.ManythankstoPedroVera-Perezfore-mailingmewiththisinfo.
AspromisedIamgoingtodiveintomoredetailaboutstoredprocedures.LetmestartoutbyansweringacommonquestionIreceivedviae-mail.Manypeoplewroteaskingifitwaspossible,andifsohowtodoit,tousestoredproceduresdotomorethanselectstatements.Absolutely!!!Anythingthatyoucanaccomplishinasqlstatementcanbeaccomplishedinastoredprocedure,simplybecauseastoredprocedurecanexecutesqlstatements.LetslookatasimpleINSERTexample.
CREATEPROCEDUREsp_myInsert
@FirstNamevarchar(20),
@LastNamevarchar(30)
As
INSERTINTONames(FirstName,LastName)
values(@FirstName,@LastName)
Go
Now,callthisprocedurewiththeparametersanditwillinsertanewrowintotheNamestablewiththeFirstNameandLastNamecolumnsapproiatelyassigned.AndhereisanexampleofhowtocallthisprocedurewithparametersfromanASPpage:
<%
dimdataConn,sSql
dimFirstName,LastName
FirstName="Nathan"
LastName="Pond"
setdataConn=Server.CreateObject("ADODB.Connection")
dataConn.Open"DSN=webData;uid=user;pwd=password"makeconnection
sSql="sp_myInsert"&FirstName&","&LastName&""
dataConn.Execute(sSql)executesqlcall
%>
Remeber,youcanusestoredproceduresforanything,includingUPDATEandDELETEcalls.Justembedasqlstatementintotheprocedure.Noticethattheaboveproceduredoesntreturnanything,soyoudontneedtosetarecordset.ThesamewillbetrueforUPDATEandDELETEcalls.TheonlystatementthatreturnsarecordsetistheSELECTstatement.
Now,justbecausearecordsetisntreturned,itdoesntmeanthattherewontbeareturnvalue.Storedprocedureshavetheabilitytoreturnsinglevalues,notjustrecordsets.Letmeshowyouapracticalexampleofthis.Supposeyouhavealoginonyoursite,theuserentersausernameandpassword,andyouneedtolooktheseupinthedatabase,iftheymatch,thenyouallowtheusertologon,otherwiseyouredirectthemtoanincorrectlogonpage.Withoutastoredproceduresyouwoulddosomethinglikethis:
<%
dimdataConn,sSql,rs
setdataConn=Server.CreateObject("ADODB.Connection")
dataConn.Open"DSN=webData;uid=user;pwd=password"makeconnection
sSql="Select*FromUser_TableWhereUserName="&_
Request.Form("UserName")&"AndPassword="&_
Request.Form("Password")&""
Setrs=dataConn.Execute(sSql)executesqlcall
Ifrs.EOFThen
Redirectuser,incorrectlogin
Response.Redirect"Incorrect.htm"
EndIf
processlogoncode
.............
%>
Nowletslookathowwewouldaccomplishthissametaskusingastoredprocedure.Firstletswritetheprocedure.
CREATEPROCEDUREsp_IsValidLogon
@UserNamevarchar(16),
@Passwordvarchar(16)
As
ifexists(Select*FromUser_Table
WhereUserName=@UserName
And
Password=@Password)
return(1)
else
</p>缺点:安全性不是太差了,还行,只要你充分利用系统自带的工具;唯一缺点就是执行效率慢,如何进行网站优化以后,效果会比较好。 交流是必要的,不管是生活还是学习我们都要试着去交流,通过交流我们可以学到很多我们自己本身所没有的知识,可以分享别人的经验甚至经历。 ASP主要是用好六个对象,其实最主要的是用好其中两个:response和request,就可以随心所欲地控制网页变换和响应用户动作了。 在平时的学习过程中要注意现学现用,注重运用,在掌握了一定的基础知识后,我们可以尝试做一些网页,也许在开始的时候我们可能会遇到很多问题,比如说如何很好的构建基本框架。 ASP.Net摆脱了以前ASP使用脚本语言来编程的缺点,理论上可以使用任何编程语言包括C++,VB,JS等等,当然,最合适的编程语言还是MS为.NetFrmaework专门推出的C(读csharp),它可以看作是VC和Java的混合体吧。 运用ASP可将VBscript、javascript等脚本语言嵌入到HTML中,便可快速完成网站的应用程序,无需编译,可在服务器端直接执行。容易编写,使用普通的文本编辑器编写,如记事本就可以完成。由脚本在服务器上而不是客户端运行,ASP所使用的脚本语言都在服务端上运行。 还有如何才能在最短的时间内学完?我每天可以有效学习2小时,双休日4小时。 跟学别的语言一样,先掌握变量,流程控制语句(就是ifwhileselect)等,函数/过程,数组 弱类型造成潜在的出错可能:尽管弱数据类型的编程语言使用起来回方便一些,但相对于它所造成的出错几率是远远得不偿失的。
页:
[1]