2009年3月7日星期六

十天学会ASP之第五天

学习目的:学会数据库的基本操作1(写入记录)
数据库的基本操作无非是:查询记录,写入记录,删除记录,修改记录。今天我们先学习写入记录。
先建立一个表单:

name

tel

message




表单提交到example5.asp,下面是example5.asp的代码:
<%
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath("example3.mdb")
name=request.form("name")
tel=request.form("tel")
message=request.form("message")
exec="insert into guestbook(name,tel,message)values('"+name+"',"+tel+",'"+message+"')"
conn.execute exec
conn.close
set conn=nothing
response.write "记录添加成功!"
%>
在这里前面两句我不说了,后面三句我也不说了,前面说过exec里面的是执行的命令,添加记录的比较繁,大家要仔细看。insert into后面加的是表的名字,后面的括号里面是需要添加的字段,不用添加的或者字段的内容就是默认值的可以省略。注意,这里的变量一定要和ACCESS里面的字段名对应,否则就会出错。values后面加的是传送过来的变量。exec是一个字符串,"insert into guestbook(name,tel,message)values('"是第一段,在ASP里面不能嵌双引号,所以可以用'代替双引号,放在双引号里面,连接两个变量用+或者&所以"',"又是一段,中间夹了一个name就是表单传来的变量,这样就可以在这个变量外面加两个'',表示是字符串了,后面的tel是数字型变量所以不需要外面包围'',大家慢慢分析这句话,如果用表单传来的数据代替变量名字的话这句话为(假设name="aaa",tel=111,message="bbb"):"insert into guestbook(name,tel,message)values('aaa',111,'bbb')"。
接下来的conn.execute 就是执行这个exec命令,最后别忘记把打开的数据库关闭,把定义的组件设置为空,这样可以返回资源。上次的读取为了简单,我没有关闭,大家可以补充上去:
rs.close
set rs=nothing
conn.close
set conn=nothing
记住,次序不可以颠倒!
大家可以到数据库里面去看一看,或者用example4.asp读取看看是不是多了记录阿?

-

资料引用:http://www.knowsky.com/4027.html

没有评论:

wholesale retail company from china

achinaseller.com is a professional wholesale retail company, having our own factory with 5 years of wholesale retail experience. Achinaseller.com is also cooperating closely with many factories, so the price that we offer to the consumer is directly from the Chinese Factory. Without leaving your home, you could order your own products at achinaseller.com at the lowest price.
We service over 100,000 registered buyers, delivering to virtually every country in the world. Our warehouse and office are staffed by a young team of Chinese and Western staff.
Happy AchinaSeller buyers include frequent wholesale importers, famous ecommerce websites, eBay powersellers, retail shops and startup home import businesses. On a daily basis, we serve corporate buyers, drop-ship experts, home business Moms, and small bussiness wholesaler