|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
SQLServer是基于服务器端的中型的数据库,可以适合大容量数据的应用,在功能上管理上也要比Access要强得多。在处理海量数据的效率,后台开发的灵活性,可扩展性等方面强大。Onewaytodoit:
Doyouliketoknowhowmanyusersvisitedyoursite?CreatingaWebcounterisveryeasythingtodo
usingASP.Theonlythingyouhavetodoistouseanapplicationservervariablecalledcount,this
variablewillhavethevaluezerowhenyourapplicationisstartedforthefirsttime,andeverytimeanew
visitorwillcometoyoursitethecountvariablewillbeincremented,theincrementsectionwillbe
writtenintheSession_OnStartintheGlobal.asathroughthreesteps:
¨Locktheapplicationserversonomorethanonevisitorwilltrytoincreaseitatthesametime.
¨Incrementthecountvariablebyone.
¨Unlockthevariable.
Andthat’sallwhatyouhavetodotocreateasimplecounterandhereisthecode.
FirstletusdotheGlobal.asasection:
<SCRIPTLANGUAGE=VBScriptRUNAT=Server>
SubSession_OnStart
LocktheApplicationobject
Application.Lock
Incrementthecount
Application("count")=Application("count")+1
UnlocktheApplicationobject
Application.Unlock
EndSub
</SCRIPT>
Andnowtoshowtheresultyoujustneedtoretrievethecountvariablelikethisinyourwebpage:
<%@language="vbscript"%>
<HTML>
<HEAD>
<TITLE>CounterExample1</TITLE>
</HEAD>
<BODY>
<H1>Youarethevisitornumber<%=Application("count")%></H1>
</BODY>
</HTML>
Anotherwaytodoit:
Okthat’sverynice,butwhatwillhappeniftheapplicationstopsforanyreason?Youwillloseallthe
datastoredintheapplicationvariablesandthatincludesthecountvariable.TosolvethisproblemI
willuseanotherway,youneedtostorethevalueofthecountvariablewithinatextfileordatabase.
FormeIpreferdatabasesoIwilluseanMSaccessdatabasewithatableofonefieldcalledcountand
thefieldcalledcounteroftypeNUMBER.
<%
Declarethevariablesthatwillbeusedwithourcode
DimConnection,RS,ConStr,Counts
Createandopenthedatabaseconnection
SetConnection=Server.Createobject("ADODB.Connection")
ConStr=("DRIVER={MicrosoftAccessDriver(*.mdb)};DBQ="&Server.MapPath("counter.mdb"))
Connection.OpenConStr
Createtherecordsetandretrivethecountervalue
SetRS=Connection.Execute("SELECT*FROMcount")
Increasethecountervaluebyone
Counts=RS("counter")+1
Updatethecountervalueinthedatabase
SetRS=Connection.Execute("UPDATEcountSETcounter="&Counts)
Connection.Close
%>
<HTML>
<HEAD>
<TITLE>CounterExample2</TITLE>
</HEAD>
<BODY>
<H1>Youarethevisitornumber<%=Counts%></H1>
</BODY>
</HTML>
WhataboutActiveusers:
Someguyswantstoknowhowmanyvisitorsarecurrentlyseeingthesite,forthatwewilluseanotherway,
wewillcreateanapplicationvariablecalledactive,andoneachnewsessionwewillincreaseitbyone
andwhenasessionendswewilldecreaseitbyone,andhereisthecode:
TheGlobal.asa:
<SCRIPTLANGUAGE="VBScript"RUNAT="Server">
SubApplication_OnStart
Createthevariablethatwillholdthenumberofactivevisitors
Application("Active")=0
EndSub
SubSession_OnStart
Increasethecounterbyone
Application.Lock
Application("Active")=Application("Active")+1
Application.UnLock
EndSub
SubSession_OnEnd
Decreasethecounter
Application.Lock
Application("Active")=Application("Active")-1
Application.UnLock
EndSub
</SCRIPT>
AndtoshowtheresultsjustcalltheApplicationvariableactiveinyourwebpagejustlikethis:
<%=Application("Active")%>
Ihopeyoucancreateyourowncountersfromnowon.Giveittryandgoodluck.
专业性的服务。有的ASP商提供垂直型的应用服务,针对某一特定行业提供应用服务。 |
|