一.VBScript语法简介
VBScript语句是一种基于VB的一种脚本语言,主要用于WEB服务器端的程序开发,我们
这里只介绍一些简单的语句,主要是操作数据库的几种常见的语句
<1>.vbscript的标识
<%
语句
……
%>
<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
4.<!--- include conn.asp--->
<%
conn.open
sql= "insert/update/delete...."
conn.execute sql
conn.close
set conn=nothing
//------------
set rs=server.createobject('adodb.recordset')
rs.open sql,conn,1,3
rs("username")="wuchengyi"
rs.update
rs.close
//------------
//pagination
sql="select * frm test"
set rs=server.createobject('adodb.recordset')
rs.open exec,conn,1,1
rs.PageSize=3// define show number
pagecount=rs.PageCount//whole number
page=int(request.QueryString("page"))
if page<=0 or page==""then page=1
rs.AbsolutePage=page
.....
for i=1 to pagecount
next
//session
Session('bb')=bb
%>
二.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
end 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对象的一般使用方法
比如:
<%
response
.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)
%>
通过以上的几句代码我们就将表单中的数据读进来了,接下来我们要做的就是将
信息写入数据库了,写入数据库的方法上面都介绍了,这里就不一一复述了。
(通过上面的学习大家完全可以自己做一个留言版了)
分享到:
相关推荐
for beginner (like me) to start up with since the graphical interface and wizards are easy to use, easy to install, easy to maintain, etc. The greatest part of IIS5 is its scalability to plug in ISAPI...
Huge ASP upload is easy to use, hi-performance ASP file upload component with progress bar indicator. This component lets you upload multiple files with size up to 4GB to a disk or a database along ...
Use behavioral driven development with ASP NET Web API to implement and enhance the application Explore techniques for building clients that are resilient to change and make it easy to consume ...
Build web applications that are easy to test and maintain Dive into the functions of controllers—the heart of an MVC site Explore the structure and behavior of a view engine Process a variety of ...
ASP.NET MVC 2.0 lets you test drive your code, control the output of your HTML, and leverage C# and .NET in an easy-to-use web framework. This book shows you all you need to know to get started ...
Microsoft® Active Server Pages (ASP) has grown from a simple scripting environment for creating dynamic Web pages into a powerful and easy-to-use platform for fully-fledged Web application ...
Over 40 styled, supercharged, and easy-to-use controls built on Web standards including AJAX, CSS, HTML5, and jQuery. All of which are available as ASP.NET Controls, Extenders and MVC tools. Shift ...
The accompanying CD includes Visual Web Developer 2005 Express, a lightweight, easy-to-use and easy-to-learn web development tool, and all projects developed in the book's examples
The accompanying CD includes Visual Web Developer 2005 Express, a lightweight, easy-to-use and easy-to-learn web development tool, and all projects developed in the book's examples
Develop next-generation web applications with ASP.NET MVC Go deep into the architecture and features of ASP.NET MVC 5, and learn how to build ...Use Responsive Web Design to make sites mobile-friendly
This book shows you how to make use of ASP.NET's features and create a functional Content Management System quickly and conveniently. You will learn how to build your site and see the different ways ...
ASP.NET Web Matrix 有关链接:http://www.asp.net/webmatrix/default.aspx?tabIndex=4&tabId=46 <br/>简介:ASP.NET Web Matrix is a community-supported, easy-to-use WYSIWYG application development tool...
<END><br>11,vpasp.zip VP-ASP is a comprehensive, ready to use ASP shopping cart for business and developers. Can be use standalone or integrated into any web. Does not require any DLLs, it is ...
Over 40 styled, supercharged, and easy-to-use controls built on Web standards including AJAX, CSS, HTML5, and jQuery. All of which are available as ASP.NET Controls, Extenders and MVC tools. Shift ...
Over 40 styled, supercharged, and easy-to-use controls built on Web standards including AJAX, CSS, HTML5, and jQuery. All of which are available as ASP.NET Controls, Extenders and MVC tools. Shift ...
Over 40 styled, supercharged, and easy-to-use controls built on Web standards including AJAX, CSS, HTML5, and jQuery. All of which are available as ASP.NET Controls, Extenders and MVC tools. Shift ...
Over 40 styled, supercharged, and easy-to-use controls built on Web standards including AJAX, CSS, HTML5, and jQuery. All of which are available as ASP.NET Controls, Extenders and MVC tools. Shift ...
40+ styled, supercharged, and easy-to-use controls built on Web standards including AJAX, CSS, HTML5, and jQuery. One Technology for All ASP.NET Development One Technology for All ASP.NET ...
40+ styled, supercharged, and easy-to-use controls built on Web standards including AJAX, CSS, HTML5, and jQuery. One Technology for All ASP.NET Development One Technology for All ASP.NET ...
40+ styled, supercharged, and easy-to-use controls built on Web standards including AJAX, CSS, HTML5, and jQuery. One Technology for All ASP.NET Development One Technology for All ASP.NET ...