1Asp初學者的一些基本代碼
1、ASP開始結束符語法:<%%>文件后綴。asp
2、判斷語句:判斷表單傳來的用戶名和密碼是否正確,并提示
Ifrequest("username")="admin"then
Response.write"恭喜,你已經登錄成功"
Else
Response.write"對不起,您輸入的用戶名錯誤,請返回重輸入"
Endif
Ifrequest("name")="admin"andrequest("pass")="admin"then
Response.redirect"admin.asp"
Else
Response.redirect"login.asp"
Endif
3循環(huán)語句:循環(huán)顯示6條數(shù)據(jù)庫中的數(shù)據(jù)
寫法1
forn=1to6
Response.writers("title")&"<br>"
ifnotrs.eofthen
exitfor
else
rs.movenext
endif
next
寫法二
dowhilenotrs.eof
Response.writers("title")&"<br>"
rs.movenext
loop
Now()函數(shù)返回系統(tǒng)時間
Date()函數(shù)返回當前系統(tǒng)時間
CStr(int)函數(shù)轉化一個表達式為字符串
CInt(string)將一個表達式轉化為數(shù)字類型
Trim(requrst("username"))函數(shù)去掉字符串左右的空格
Lift(rs("title"),10)函數(shù)返回字符串左邊第length個字符以前的字符(含第length個字符),一般限制新聞標題的顯示長度的時候用
Len(string)函數(shù)返回字符串的長度
5:ACCESS
方法一:
db="mydata.mdb"'如果放在目錄中,就要寫明"database/mydata.mdb"
Setconn=Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="&
Server.MapPath(db)
conn.Openconnstr
方法二
db="mydata.mdb"'如果放在目錄中,就要寫明"database/mydata.mdb"
Setconn=Server.CreateObject("ADODB.Connection")
connstr="driver={MicrosoftAccessDriver(*.mdb)};dbq="&
Server.MapPath(db)
conn.Openconnstr
6:Recordset對象操作數(shù)據(jù)庫語法
(1)打開sql語句指定的表中的數(shù)據(jù),把這批數(shù)據(jù)放入rs對象中
取出news表中所有的數(shù)據(jù)放到rs中
SetRs=server.CreateObject("ADODB.RecordSet")
SqlStr="select*fromnews"
Rs.OpenSqlStr,conn1,1
取出news表中簽條數(shù)據(jù)放到rs中
SetRs=Server.CreateObject("ADODB.RecordSet")
SqlStr="selecttop6*fromnews"
Rs.OpenSqlStr,conn1,1
(2)循環(huán)顯示6條rs對象中存在的數(shù)據(jù),列表顯示不帶連接的寫法
forn=1to6
Response.writers("title")&"<br>"
ifnotrs.eofthen
exitfor
else
rs.movenext
endif
next
帶連接的寫法
forn=1to6
Response.write"<ahref=show.asp?id=rs("id")>"&left(rs("title"),20)&"</a><br>"
ifnotrs.eofthen
exitfor
else
rs.movenext
endif
next
(3)向數(shù)據(jù)庫中添加一條數(shù)據(jù)的代碼
SetRs=server.CreateObject("ADODB.RecordSet")
SqlStr="select*fromnews"
Rs.OpenSqlstr,conn,1,3'注意這里的1,3代表可以寫入的打開數(shù)據(jù)表
Rs.addnew
Rs("title")=trim(request("title"))
Rs("neirong")=request("neirong")
Rs("date")=new()
rs.update'真正寫入數(shù)據(jù)庫
(4)修改一條記錄的代碼,通過(2)中的連接傳遞過來id數(shù)值
SetRs=server.CreateObject("ADODB.RecordSet")
SqlStr="select*fromnewswhereid="&request(id)
Rs.OpenSqlstr,conn,1,3'注意這里的1,3代表可以寫入的打開數(shù)據(jù)表
Rs("title")=trim(request("title"))
Rs("neirong")=request("neirong")
Rs("date")=new()
rs.update'真正寫入數(shù)據(jù)庫
(5)刪除數(shù)據(jù)庫中的一條記錄,通過連接傳遞過來了數(shù)據(jù)得id數(shù)值
SetRs=server.CreateObject("ADODB.RecordSet")
SqlStr="select*fromnewswhereid="&request(id)
Rs.OpenSqlStr,conn,1,3'注意這里的1,3代表可以寫入的打開數(shù)據(jù)表
2Asp初學者的一些基本代碼
rs.delete'刪除該條數(shù)據(jù)
7:標準SQL語句寫法
包括取全部記錄
SetRs=server.CreateObject("ADODB.RecordSet")
SqlStr="select*fromnews"
Rs.OpenSqlStr,conn,1,1‘運行sql語句,把數(shù)據(jù)提出到rs對象中
選取幾條數(shù)據(jù)
SetRs=Server.CreateObject("ADODB.RecordSet")
SqlStr="selecttop6*fromnews"
Rs.OpenSqlStr,conn1,1'運行sql語句,把6條數(shù)據(jù)提出到rs對象中選取一條指定表中id字段數(shù)值的數(shù)據(jù)
SetRs=Server.CreateObject("ADODB.RecordSet")
SqlStr="select*fromnewswhereid="&request("id")
Rs.OpenSqlStr,conn1,1'運行sql語句,把6條數(shù)據(jù)提出到rs對象中添加一條表單傳過來的數(shù)據(jù)替換
SetRs=Server.CreateObject("ADODB.RecordSet")
SqlStr="insertintonews(title,neirong)values(request("title"),request("neirong"))"
修改一條指定表中id字段數(shù)值的數(shù)據(jù),用表單傳來的數(shù)據(jù)替換
SetRs=Server.CreateObject("ADODB.RecordSet")
SqlStr="updatenewssettitle='"&request("title")&"',neirong='"request("內容")&"'whereid="&request("id")"
Rs.OpenSqlStr,conn1,3'運行sql語句,把數(shù)據(jù)提出到rs對象中,刪除一條指定表中id字段數(shù)值的數(shù)據(jù)
SetRs=Server.CreateObject("ADODB.RecordSet")
SqlStr="deletefromnewswhereid="&request("id")
Rs.OpenSqlStr,conn1,3'運行sql語句,把數(shù)據(jù)提出到rs對象中
8、當單擊按鈕時侯表單帶著的數(shù)據(jù)傳送到哪個文件,在那里指定?
<formmethod="post"action="addsave.asp">
<inputtype="text"name="title">
<inputtype="text"name="neirong">
<inputtype="submit"name="Submit"value="提交">
</from>
9、表單提交來的數(shù)據(jù)接受并顯示到屏幕上的代碼
response.writerequest("name")
response.writenow()
response.writetrim(requst("name"))
10,利用Application對象作計數(shù)器的語法
在網(wǎng)頁的頭部加入
Application.Lock
Application("counter")=Application("counter")+1
Application.UnLock
在需要顯示計數(shù)內容的網(wǎng)頁的地方,加入下面的語句
response.writeApplication("counter")
11,利用Session對象保護后臺管理頁面admin.asp,防止未登陸用戶進入
ifsession(admin)<>"ok"then
response.redirect"login.asp"
response.end
endif
在網(wǎng)站后臺登陸頁的密碼驗證部分標準寫法
AdmName=Request.Form("Name")
AdmName=Request.Form("Pass")
SetRs=Server.CreateObject("ADODB.RecordSet")
SqlStr="Select*fromAdminwherename='"&AdmName&"'and
pass='"&AdmPass&"'"
Rs.OpenSqlStr,conn,1,3
ifRs.EOFANDRS.BOFthen
Response.Redirect("login.asp")
response.end
else
session("admin")="ok"
Response.Redirect("admin.asp")
response.end
endif
12、分頁代碼
sql="select..................省略了sql語句
Setrs=Sevrer.Createobject("ADODB.RECORDSEAT")
rs.Opensqlconn1,1
ifnotrs.eofthen
pages=30'定義每頁顯示的記錄數(shù)
rs.pageSize=page'定義每頁顯示的記錄數(shù)
allPages=rs.pageCount'計算一共能分多少頁
page=Request.QueryString("page")'通過瀏覽器傳遞的頁數(shù)
’if語句屬于基本的排錯處理
ifisEmpty(page)orCint(page)<1then
page=1
elseifCint(page)>allpagesthen
page=mallpages
endif
rs.AbsolutePage=page
Dowhilenotrs.eofandpages>0
'這里輸出你要的內容。。。。。。。。。。。。。。。
pages=pages-1
rs.MoveNext
3Asp初學者的一些基本代碼
Loop
esle
Response.Write("數(shù)據(jù)庫暫時無內容!")
Endif
rs.Close
Setrs=Nothing
分頁頁碼連接和跳轉頁碼程序
<fromAction="v3.asp"Method="GET">
<%
Ifpage<>1then
Response.Write"(AHREF=?Page=1>第一頁</A>"
Response.Write"(AHREF=?Page="&(page-1)&">上一頁</A>
Endif
IfPage<>rs.PageCountthen
Response.Write"(AHREF=?Page="&(page+1)&">下一頁</A>
Response.Write"(AHREF=?Page="&rs.PageCount&">最后一頁</A>
Endif
%>
<p>輸入頁數(shù):<inputTYPE="TEXT"Name="Page"SIZE="3">頁數(shù):<fontCOLOR="Red"><%=Page%>/<%=rs.PageCount%></font>
</p>
</from>
13分行列顯示圖片和名稱的代碼(4列*3行=12個)
<%
SetRs=Server.CreateObject("ADODB.RecordSet")
SqlStr="selecttop12*frommyprduct"
Rs.OpenSqlStr,coon1,1
i=1
%>
<tablewidth="90%"border="1"cellspacing="0"sellpadding="0">
tr>
<%
dowhilenotrs.eof
%>
<tdalign="center">
<imgsrc="<%=rs("imgurl")%>"width="52"height="120"><br>
<%=rs("productname")%>
</td>
<%ifimod4=0thenresponse.write"</tr><tr>"
i=i+1
rs.movenext
loop
rs.close
%>
</tr>
</table>
十四,ASP數(shù)據(jù)庫連接之ACCESS-SQLSERVER
<%
IsSq1Data定義數(shù)據(jù)庫類別,0為Access數(shù)據(jù)庫,1為SQL數(shù)據(jù)庫
IfIsSqData=0Then
Access數(shù)據(jù)庫
datapath="data/"數(shù)據(jù)庫目錄的相對路徑
datafile="data.mdb"數(shù)據(jù)庫的文件名
connstr="provider=Microsoft.jet.OLEDB.4.0;DataSource="&Server.MapPath(""&datapath&""&datafile&"")
Connstr="DBQ"="&server.mappath(""&datapath&""&datafile&"")&";DRIVER={MicosoftAccessDriver(*.dbm)};"
Esle
SQL數(shù)據(jù)庫
SqlLocalName="(local)"連接IP[本地用(local)外地用IP]
SqlUsername="sa"用戶名
Sqlpassword="1"
SqlDatabaseName="data"數(shù)據(jù)庫名
ConnStr="Provider=Sqloledb;UserID="&SqlUsername&";Password="&SqlPassword&";InitialCatalog="&SqlDatabaseName&";DataSource="&SqlLocalName&";"
ENDIF
OnErrorResumeNext
Steconn=Server.CreateObject(ADODB.Connection)
conn.openConnStr
IfErrThen
err.Clear
SetConn=Nothing
esponse.Write"數(shù)據(jù)庫連接出錯,請檢查連接字串。"
Response.End
EndIf
OnErrorGoTo0
%>
一.ASP的常用語句
<1>規(guī)范語句
<%
語句
……
%>
<2>定義變量dim語句
<%
dima,b
a=10
b=”ok!”
%>
注意:定義的變量可以是數(shù)值型,也可以是字符或者其他類型的
<3>簡單的控制流程語句
1.If條件1then
語句1
elseif條件2then
語句2
else
語句3
endif
2.while條件
語句
wend
3.forcount=1tonstepm
語句1
exitfor
語句2
next
二.ASP數(shù)據(jù)庫簡單操作教程
<1>.數(shù)據(jù)庫連接(用來單獨編制連接文件conn.asp)
<%
Setconn=Server.CreateObject("ADODB.Connection")
conn.Open"DRIVER={MicrosoftAccessDriver(*.mdb)};DBQ="&Server.MapPath("bbsdb1user.mdb")
%>
(用來連接bbsdb1目錄下的user.mdb數(shù)據(jù)庫)
<2>顯示數(shù)據(jù)庫記錄
原理:將數(shù)據(jù)庫中的記錄一一顯示到客戶端瀏覽器,依次讀出數(shù)據(jù)庫中的每一條記錄
如果是從頭到尾:用循環(huán)并判斷指針是否到末使用:notrs.eof
如果是從尾到頭:用循環(huán)并判斷指針是否到開始使用:notrs.bof
<!--#includefile=conn.asp-->(包含conn.asp用來打開bbsdb1目錄下的user.mdb數(shù)據(jù)庫)
<%
setrs=server.CreateObject("adodb.recordset")(建立recordset對象)
sqlstr="select*frommessage"---->(message為數(shù)據(jù)庫中的一個數(shù)據(jù)表,即你要顯示的數(shù)據(jù)所存放的數(shù)據(jù)表)
rs.opensqlstr,conn,1,3---->(表示打開數(shù)據(jù)庫的方式)
rs.movefirst---->(將指針移到第一條記錄)
whilenotrs.eof---->(判斷指針是否到末尾)
response.write(rs("name"))---->(顯示數(shù)據(jù)表message中的name字段)
rs.movenext---->(將指針移動到下一條記錄)
wend---->(循環(huán)結束)
------------------------------------------------------
rs.close
conn.close這幾句是用來關閉數(shù)據(jù)庫
setrs=nothing
setconn=nothing
-------------------------------------------------------
%>
其中response對象是服務器向客戶端瀏覽器發(fā)送的信息
<3>增加數(shù)據(jù)庫記錄
增加數(shù)據(jù)庫記錄用到rs.addnew,rs.update兩個函數(shù)
<!--#includefile=conn.asp-->(包含conn.asp用來打開bbsdb1目錄下的user.mdb數(shù)據(jù)庫)
<%
setrs=server.CreateObject("adodb.recordset")(建立recordset對象)
sqlstr="select*frommessage"---->(message為數(shù)據(jù)庫中的一個數(shù)據(jù)表,即你要顯示的數(shù)據(jù)所存放的數(shù)據(jù)表)
rs.opensqlstr,conn,1,3---->(表示打開數(shù)據(jù)庫的方式)
rs.addnew新增加一條記錄
rs("name")="xx"將xx的值傳給name字段
rs.update刷新數(shù)據(jù)庫
------------------------------------------------------
rs.close
conn.close這幾句是用來關閉數(shù)據(jù)庫
setrs=nothing
setconn=nothing
-------------------------------------------------------
%>
<4>刪除一條記錄
刪除數(shù)據(jù)庫記錄主要用到rs.delete,rs.update
<!--#includefile=conn.asp-->(包含conn.asp用來打開bbsdb1目錄下的user.mdb數(shù)據(jù)庫)
<%
dimname
name="xx"
setrs=server.CreateObject("adodb.recordset")(建立recordset對象)
sqlstr="select*frommessage"---->(message為數(shù)據(jù)庫中的一個數(shù)據(jù)表,即你要顯示的數(shù)據(jù)所存放的數(shù)據(jù)表)
rs.opensqlstr,conn,1,3---->(表示打開數(shù)據(jù)庫的方式)
-------------------------------------------------------
whilenotrs.eof
ifrs.("name")=namethen
rs.delete
rs.update查詢數(shù)據(jù)表中的name字段的值是否等于變量name的值"xx",如果符合就執(zhí)行刪除,
else否則繼續(xù)查詢,直到指針到末尾為止
rs.movenext
emdif
wend
------------------------------------------------------
------------------------------------------------------
rs.close
conn.close這幾句是用來關閉數(shù)據(jù)庫
setrs=nothing
setconn=nothing
-------------------------------------------------------
%>
<5>關于數(shù)據(jù)庫的查詢
(a)查詢字段為字符型
<%
dimuser,pass,qq,mail,message
user=request.form("user")
pass=request.form("pass")
qq=request.form("qq")
mail=request.form("mail")
message=request.form("message")
iftrim(user)&"x"="x"ortrim(pass)&"x"="x"then(檢測user值和pass值是否為空,可以檢測到空格)
response.write("注冊信息不能為空")
else
setrs=server.CreateObject("adodb.recordset")
sqlstr="select*fromuserwhereuser='"&user&"'"(查詢user數(shù)據(jù)表中的user字段其中user字段為字符型)
rs.opensqlstr,conn,1,3
ifrs.eofthen
rs.addnew
rs("user")=user
rs("pass")=pass
rs("qq")=qq
rs("mail")=mail
rs("message")=message
rs.update
rs.close
conn.close
setrs=nothing
setconn=nothing
response.write("注冊成功")
endif
rs.close
conn.close
setrs=nothing
setconn=nothing
response.write("注冊重名")
%>
(b)查詢字段為數(shù)字型
<%
dimnum
num=request.form("num")
setrs=server.CreateObject("adodb.recordset")
sqlstr="select*frommessagewhereid="&num(查詢message數(shù)據(jù)表中id字段的值是否與num相等,其中id為數(shù)字型)
rs.opensqlstr,conn,1,3
ifnotrs.eofthen
rs.delete
rs.update
rs.close
conn.close
setrs=nothing
setconn=nothing
response.write("刪除成功")
endif
rs.close
conn.close
setrs=nothing
setconn=nothing
response.write("刪除失敗")
%>
<6>幾個簡單的asp對象的講解
response對象:服務器端向客戶端發(fā)送的信息對象,包括直接發(fā)送信息給瀏覽器,重新定向URL,或設置cookie值
request對象:客戶端向服務器提出的請求
session對象:作為一個全局變量,在整個站點都生效
server對象:提供對服務器上方法和屬性的訪問
(a)response對象的一般使用方法
比如:
<%
resposne.write("hello,welcometoasp!")
%>
在客戶端瀏覽器就會看到hello,welcometoasp!這一段文字
<%
response.Redirect("www.sohu.com")
%>
如果執(zhí)行這一段,則瀏覽器就會自動連接到“搜狐”的網(wǎng)址
關于response對象的用法還有很多,大家可以研究研究
request對象的一般使用方法
比如客戶端向服務器提出的請求就是通過request對象來傳遞的
列如:你在申請郵箱的所填寫的個人信息就是通過該對象來將
你所填寫的信息傳遞給服務器的
比如:這是一段表單的代碼,這是提供給客戶填寫信息的,填寫完了按
“提交”傳遞給request.asp文件處理后再存入服務器數(shù)據(jù)庫
<formname="form1"method="post"action="request.asp">
<p>
<inputtype="text"name="user">
</p>
<p>
<inputtype="text"name="pass">
</p>
<p>
<inputtype="submit"name="Submit"value="提交">
</p>
</form>
那么request.asp該如何將其中的信息讀入,在寫入數(shù)據(jù)庫,在這里就要用到
request對象了,下面我們就來分析request.asp的寫法
<%
dimname,password(定義user和password兩個變量)
name=request.form(“user”)(將表單中的user信息傳給變量name)
password=request.form(“pass”)(將表單中的pass信息傳給變量password)
%>
通過以上的幾句代碼我們就將表單中的數(shù)據(jù)讀進來了,接下來我們要做的就是將
信息寫入數(shù)據(jù)庫了,寫入數(shù)據(jù)庫的方法上面都介紹了,這里就不一一復述了。
(通過上面的學習大家完全可以自己做一個留言版了)
愛華網(wǎng)



