ASP入门

[ 2004-09-08 18:36:43 | Author: zhenhua ]
Font Size: Large | Medium | Small
<1>格式
<%
语句
......
%>
<2>定义变量dim语句
<%
dim a,b
a=10
b="ok!"
%>
注意:定义的变量可以是数值型,也可以是字符或者其他类型的
<3>简单的控制流程语句
1. If 条件1 then
语句1
elseif 条件2 then
语句2
else
语句3
endif
2.while 条件
语句
wend
3.for count=1 to n step m
语句1
exit for
语句2
next
二.ASP数据库简单*作教程
<1>.数据库连接(用来单独编制连接文件conn.asp)
<%
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath
("\bbs\db1\user.mdb")
%>
(用来连接bbs\db1\目录下的user.mdb数据库)
<2>显示数据库记录
原理:将数据库中的记录一一显示到客户端浏览器,依次读出数据库中的每一条记录
如果是从头到尾:用循环并判断指针是否到末 使用: not rs.eof
如果是从尾到头:用循环并判断指针是否到开始 使用:not rs.bof

<!--#include file=conn.asp--> (包含conn.asp用来打开bbs\db1\目录下的user.mdb数据
库)
<%
set rs=server.CreateObject("adodb.recordset") (建立recordset对象)
sqlstr="select * from message" ---->(message为数据库中的一个数据表,即你要显示的
数据所存放的数据表)
rs.open sqlstr,conn,1,3 ---->(表示打开数据库的方式)
rs.movefirst ---->(将指针移到第一条记录)
while not rs.eof ---->(判断指针是否到末尾)
response.write(rs("name")) ---->(显示数据表message中的name字段)
rs.movenext ---->(将指针移动到下一条记录)
wend ---->(循环结束)
------------------------------------------------------
rs.close
conn.close 这几句是用来关闭数据库
set rs=nothing
set conn=nothing
-------------------------------------------------------
%>
其中response对象是服务器向客户端浏览器发送的信息
<3>增加数据库记录
增加数据库记录用到rs.addnew,rs.update两个函数
<!--#include file=conn.asp--> (包含conn.asp用来打开bbs\db1\目录下的user.mdb数据
库)
<%
set rs=server.CreateObject("adodb.recordset") (建立recordset对象)
sqlstr="select * from message" ---->(message为数据库中的一个数据表,即你要显示的
数据所存放的数据表)
rs.open sqlstr,conn,1,3 ---->(表示打开数据库的方式)
rs.addnew 新增加一条记录
rs("name")="xx" 将xx的值传给name字段
rs.update 刷新数据库
------------------------------------------------------
rs.close
conn.close 这几句是用来关闭数据库
set rs=nothing
set conn=nothing
-------------------------------------------------------

%>
<4>删除一条记录
删除数据库记录主要用到rs.delete,rs.update
<!--#include file=conn.asp--> (包含conn.asp用来打开bbs\db1\目录下的user.mdb数据库)
<%
dim name
name="xx"
set rs=server.CreateObject("adodb.recordset") (建立recordset对象)
sqlstr="select * from message" ---->(message为数据库中的一个数据表,即你要显示的
数据所存放的数据表)
rs.open sqlstr,conn,1,3 ---->(表示打开数据库的方式)
-------------------------------------------------------
while not rs.eof
if rs.("name")=name then
rs.delete
rs.update 查询数据表中的name字段的值是否等于变量name的值"xx",如果符
合就执行删除,
else 否则继续查询,直到指针到末尾为止
rs.movenext
emd if
wend
------------------------------------------------------
------------------------------------------------------
rs.close
conn.close 这几句是用来关闭数据库
set rs=nothing
set conn=nothing
-------------------------------------------------------
%>
<5>关于数据库的查询
(a) 查询字段为字符型
<%
dim user,pass,qq,mail,message
user=request.Form("user")
pass=request.Form("pass")
qq=request.Form("qq")
mail=request.Form("mail")
message=request.Form("message")
if trim(user)&"x"="x" or trim(pass)&"x"="x" then (检测user值和pass值是否为空,可以
检测到空格)
response.write("注册信息不能为空")
else
set rs=server.CreateObject("adodb.recordset")
sqlstr="select * from user where user='"&user&"'" (查询user数据表中的user字段其中
user字段为字符型)
rs.open sqlstr,conn,1,3
if rs.eof then
rs.addnew
rs("user")=user
rs("pass")=pass
rs("qq")=qq
rs("mail")=mail
rs("message")=message
rs.update
rs.close
conn.close
set rs=nothing
set conn=nothing
response.write("注册成功")
end if
rs.close
conn.close
set rs=nothing
set conn=nothing
response.write("注册重名")
%>
(b)查询字段为数字型
<%
dim num
num=request.Form("num")
set rs=server.CreateObject("adodb.recordset")
sqlstr="select * from message where id="&num (查询message数据表中id字段的值是否与
num相等,其中id为数字型)
rs.open sqlstr,conn,1,3
if not rs.eof then
rs.delete
rs.update
rs.close
conn.close
set rs=nothing
set conn=nothing
response.write("删除成功")
end if
rs.close
conn.close
set rs=nothing
set conn=nothing
response.write("删除失败")
%>
<6>几个简单的asp对象的讲解
response对象:服务器端向客户端发送的信息对象,包括直接发送信息给浏览器,重新定向URL,
或设置cookie值
request对象:客户端向服务器提出的请求
session对象:作为一个全局变量,在整个站点都生效
server对象:提供对服务器上方法和属性的访问
(a) response对象的一般使用方法
比如:
<%
resposne.write("hello, welcome to asp!")
%>
在客户端浏览器就会看到 hello, welcome to asp! 这一段文字
<%
response.Redirect("www.sohu.com")
%>
如果执行这一段,则浏览器就会自动连接到 "搜狐" 的网址
关于response对象的用法还有很多,大家可以研究研究
request对象的一般使用方法
比如客户端向服务器提出的请求就是通过request对象来传递的
列如 :你在申请邮箱的所填写的个人信息就是通过该对象来将
你所填写的信息传递给服务器的
比如:这是一段表单的代码,这是提供给客户填写信息的,填写完了按
"提交"传递给request.asp文件处理后再存入服务器数据库
<form name="form1" method="post" action="request.asp">
<p>
<input type="text" name="user">
</p>
<p>
<input type="text" name="pass">
</p>
<p>
<input type="submit" name="Submit" value="提交">
</p>
</form>
那么request.asp该如何将其中的信息读入,在写入数据库,在这里就要用到
request对象了,下面我们就来分析request.asp的写法
<%
dim name,password (定义user和password两个变量)
name=request.form("user") (将表单中的user信息传给变量name)
password=request.form("pass") (将表单中的pass信息传给变量password)
%>
通过以上的几句代码我们就将表单中的数据读进来了,接下来我们要做的就是将
信息写入数据库了,写入数据库的方法上面都介绍了,这里就不一一复述了。

========================================
ASP获得网卡的MAC地址
<% Option Explicit%>

<%
Private Const NCBASTAT = &H33
Private Const NCBNAMSZ = 16
Private Const HEAP_ZERO_MEMORY = &H8
Private Const HEAP_GENERATE_EXCEPTIONS = &H4
Private Const NCBRESET = &H32

Private Type NCB
ncb_command As Byte 'Integer
ncb_retcode As Byte 'Integer
ncb_lsn As Byte 'Integer
ncb_num As Byte ' Integer
ncb_buffer As Long 'String
ncb_length As Integer
ncb_callname As String * NCBNAMSZ
ncb_name As String * NCBNAMSZ
ncb_rto As Byte 'Integer
ncb_sto As Byte ' Integer
ncb_post As Long
ncb_lana_num As Byte 'Integer
ncb_cmd_cplt As Byte 'Integer
ncb_reserve(9) As Byte ' Reserved, must be 0
ncb_event As Long
End Type
Private Type ADAPTER_STATUS
adapter_address(5) As Byte 'As String * 6
rev_major As Byte 'Integer
reserved0 As Byte 'Integer
adapter_type As Byte 'Integer
rev_minor As Byte 'Integer
duration As Integer
frmr_recv As Integer
frmr_xmit As Integer
iframe_recv_err As Integer
xmit_aborts As Integer
xmit_success As Long
recv_success As Long
iframe_xmit_err As Integer
recv_buff_unavail As Integer
t1_timeouts As Integer
ti_timeouts As Integer
Reserved1 As Long
free_ncbs As Integer
max_cfg_ncbs As Integer
max_ncbs As Integer
xmit_buf_unavail As Integer
max_dgram_size As Integer
pending_sess As Integer
max_cfg_sess As Integer
max_sess As Integer
max_sess_pkt_size As Integer
name_count As Integer
End Type
Private Type NAME_BUFFER
name As String * NCBNAMSZ
name_num As Integer
name_flags As Integer
End Type
Private Type ASTAT
adapt As ADAPTER_STATUS
NameBuff(30) As NAME_BUFFER
End Type

Private Declare Function Netbios Lib "netapi32.dll" _
(pncb As NCB) As Byte
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
Private Declare Function GetProcessHeap Lib "kernel32" () As Long
Private Declare Function HeapAlloc Lib "kernel32" _
(ByVal hHeap As Long, ByVal dwFlags As Long, _
ByVal dwBytes As Long) As Long
Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, _
ByVal dwFlags As Long, lpMem As Any) As Long

Public Function GetMACAddress(sIP As String) As String
Dim sRtn As String
Dim myNcb As NCB
Dim bRet As Byte

Dim aIP() As String
Dim x As Long
Dim nIP As String

If InStr(sIP, ".") = 0 Then
GetMACAddress = "无效的IP地址."
Exit Function
End If

aIP = Split(sIP, ".", -1, vbTextCompare)
If UBound(aIP()) <> 3 Then
GetMACAddress = "无效的IP地址."
Exit Function
End If

For x = 0 To UBound(aIP())
If Len(aIP(x)) > 3 Then
GetMACAddress = "无效的IP地址"
Exit Function
End If

If IsNumeric(aIP(x)) = False Then
GetMACAddress = "无效的IP地址"
Exit Function
End If

If InStr(aIP(x), ",") <> 0 Then
GetMACAddress = "无效的IP地址"
Exit Function
End If

If CLng(aIP(x)) > 255 Then
GetMACAddress = "无效的IP地址"
Exit Function
End If

If nIP = "" Then
nIP = String(3 - Len(aIP(x)), "0") & aIP(x)
Else
nIP = nIP & "." & String(3 - Len(aIP(x)), "0") & aIP(x)
End If
Next

sRtn = ""
myNcb.ncb_command = NCBRESET
bRet = Netbios(myNcb)
myNcb.ncb_command = NCBASTAT
myNcb.ncb_lana_num = 0
myNcb.ncb_callname = nIP & Chr(0)

Dim myASTAT As ASTAT, tempASTAT As ASTAT
Dim pASTAT As Long
myNcb.ncb_length = Len(myASTAT)

pASTAT = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or HEAP_ZERO_MEMORY, myNcb.ncb_length)
If pASTAT = 0 Then
GetMACAddress = "memory allcoation failed!"
Exit Function
End If

myNcb.ncb_buffer = pASTAT
bRet = Netbios(myNcb)

If bRet <> 0 Then
GetMACAddress = "不能从当前IP地址获得MAC,当前IP地址: " & sIP
Exit Function
End If

CopyMemory myASTAT, myNcb.ncb_buffer, Len(myASTAT)

Dim sTemp As String
Dim i As Long
For i = 0 To 5
sTemp = Hex(myASTAT.adapt.adapter_address(i))
If i = 0 Then
sRtn = IIf(Len(sTemp) < 2, "0" & sTemp, sTemp)
Else
sRtn = sRtn & Space(1) & IIf(Len(sTemp) < 2, "0" & sTemp, sTemp)
End If
Next
HeapFree GetProcessHeap(), 0, pASTAT
GetMACAddress = sRtn
End Function
%>
<%
set S_MAC = server.CreateObject( "adodb.recordset")
response.write S_MAC.GetMACAddress(Request.Servervariables("REMOTE_HOST"))
set S_MAC = nothing
%>

Comments Feed Comments Feed: http://www.zhenhua.org/feed.asp?q=comment&id=147
UTF-8 Encoding Trackback URL: http://www.zhenhua.org/trackback.asp?id=147

There is no comment on this article.

If you feel this site you find this information helpful, please click on the donation, which is voluntary,Thank you.
Post Comment
Smilies
[arrow] [biggrin] [confused] [cool]
[cry] [eek] [evil] [exclaim]
[frown] [idea] [lol] [mad]
[mrgreen] [neutral] [question] [razz]
[redface] [rolleyes] [sad] [smile]
[surprised] [twisted] [wink]
Enable UBB Codes
Auto Convert URL
Show Smilies
Hidden Comment
Username:   Password:   Register Now?
Security Code * Please Enter the Security Code