ASP教程之怎样在ASP中利用mySQL
对于中小型web应用来说,php有很强的竞争力,linux+apache+mysql+php(lamp)的组合几乎可以胜任绝大多数网站的解决方案,对于大型应用来讲,对于系统架构要求更高,需要有成熟的框架支持,jsp的struts是个不错的框架,国内介绍它的资料也非常多,应用逐渐广泛起来。asp就不用说了,UsingAmySQLDatabasesbyBenONeill
Databasesarethebestwaytokeepyourwebsiteup-to-dateanddynamic.Databasesareusedthesedaysby
thousandsofwebsites.Theyareusedforstoringnewsandgeneralinformation.WebsitesliketheASP
Index(www.aspin.com)arerunonlargedatabases.Databasesmakeawebsiteeasytoupdateandonceyou
havethebasescript,toadd,removeandmodifythingsinadatabaseisveryeasy.
Tostartyouneedtoknowhowtoconnecttoadatabase.ASPcanconnecttovirtuallyanytype,from
MicrosoftAccesstoSQL.InthisexampleIllbeusingmySQLandOLEDBtoconnecttoit.
mySQLcanbedownloadedfromthemySQLwebsite(www.mysql.com).Youwillalsoneedtheproviderusedto
connecttoit,alsoavailablefromthemySQLwebsite.
YoumaybeaskingwhatsOLEDB?ImusedtoODBCandDSN.OLEDBisfasterandmorestable.Itsalmost
exactlythesame.
Firstweneedtoconnecttothedatabase,becauseitsamySQLdatabaseyoualsoneedtosupplyadatabase
name.(inmySQLyoucanhavemulitpledatabasesonthesameSQLserver.)
<%
strConnection="driver={MySQL};server=localhost;uid=benoneill;pwd=mypassword;database=databasename"
SetadoDataConn=Server.CreateObject("ADODB.Connection")
adoDataConn.OpenstrConnection
%>
Andnowweveconnected.Letspretendwevegotabiglistoflotsandlotsofemailaddresses,heres
thecontentsofourdatabase,itallowsmetoshowyouhowitworksbetter.
TableName:emailadds
name??emailadd
------------------------------------------------------
Ben??sheepcow@planetunreal.com
Fred?freddy@thebigisp.com
BenHarding?benharding@hisisp.com
DaveGeralding?daveg@mymail.com
Nowwehavethedatabaseopenletsrunaquerytolistandoutputallthenamesandemailaddressina
niceeasytoviewtable.
<%
?strQuery="SELECT*FROMemailadds"
?SetrsEmailData=adoDataConn.Execute(strQuery)
?IfNotrsEmailData.BOFThen
%>
<TABLE>
?/span><TR>
<TD<b>Name</b></TD>
<TD><b>EmailAddress</b></TD>
?/span></TR>
<%
?DoWhileNotrsEmailData.EOF
%>
?/span><TR>
<TD><%=rsEmailData("name").Value%></TD>
<TD><%=rsEmailData("emailadd").Value%></TD>
?/span></TR>
<%
?rsEmailData.MoveNext
?Loop
%>
</TABLE>
<%
?Else
?Response.Write("Sorry,noemailaddressesfound.")
?EndIf
%>
Therewego.Ifnorecordsarefoundthenitsays"Sorry,noemailaddressesfound".
Thatqueryissimpleenough,ittellsthedatabasetoget(SELECT)alltherecordsandallthefieldsfrom
thetablenamedemailadds.
Howaboutwemakeitonlyshowpeoplewiththename"ben"somewhereintheirname,simplechangethequery
tothis:
SELECT*FROMemailaddsWHEREnameLIKE%ben%
Thatquerywouldreturnonly2records,BenandBenHarding.
Itsimportantyouusesinglequotes(),becausedoublequoteswontwork.Youcanalsobeveryselective
anddo:
SELECT*FROMemailaddsWHEREname=Ben
ThatquerywouldonlyreturnBen,notFredorBenHarding,orDaveGeralding.
Afterusingdatabasesyoushouldalwaysclearup.Closethedatabaseandtherecordset,andsetthemto
nothingsothatthememoryusedbythemisregained,dothisbywritingthis:
<%
?rsEmailData.Close
?adoDataConn.Close
?SetadoDataConn=Nothing
?SetrsEmailData=Nothing
%>
Windows本身的所有问题都会一成不变的也累加到了它的身上。安全性、稳定性、跨平台性都会因为与NT的捆绑而显现出来; 虽然ASP也有很多网络教程。但是这些都不系统。都是半路出家,只是从一个例子告诉你怎么用。不会深入讨论,更不会将没有出现在例子里的方法都一一列举出来。 代码的可重用性差:由于是面向结构的编程方式,并且混合html,所以可能页面原型修改一点,整个程序都需要修改,更别提代码重用了。 在平时的学习过程中要注意现学现用,注重运用,在掌握了一定的基础知识后,我们可以尝试做一些网页,也许在开始的时候我们可能会遇到很多问题,比如说如何很好的构建基本框架。 如何学好ASP,以前也有人问过,把回答给你转过来看看能否对你有帮助: 如何学好ASP,以前也有人问过,把回答给你转过来看看能否对你有帮助: 不是很难但是英文要有一点基础网上的教程很少有系统的详细的去买书吧,另不用专门学习vb关于vbscript脚本在asp教材都有介绍 他的语法和设计思路和VB完全相同,导致很多ASP的书都留一句“相关内容请参考VB的相关教材....”更糟糕的是,相当多的ASP教程混合了Javascript,VBscript等等脚本语言,搞的初学者。
页:
[1]