Username: Password:

ASP讲座之七:ASP和数据库(二)-ASP教程,数据库相关
来源:作者: 发布时间:2007-12-25 13:34:31

在上一讲中,我们学习了怎样和数据库建立连接和从数据库中检索数据,今天的内容是怎样向数据库中添加新数据、修改和删除数据库中的数据。

一、 向数据库中添加新数据
方法一:使用sql语句,例wuf50.asp。
为了简化以后的程式,将和access数据库的连接部分放在一个文档中,此文档以后需要用到时不再说明。
<% adoaccess.asp
option explicit
response.expires = 0
第一部分: 建立连接
dim cnn, strcnn
set cnn = server.createobject("adodb.connection")
strcnn = "provider = microsoft.jet.oledb.4.0; data source = c:\inetpub\home\asp\northwind.mdb"
cnn.open strcnn
%>
程式wuf50.asp
<% @language = vbscript %>

<% wuf50.asp
第二部分: 使用 connection 对象的 execute 添加新数据
dim strsql, rstest
strsql = "insert into 运货商 (公司名称,电话) values(wufeng,0571-7227298)"
cnn.execute strsql
%>


<% 第三部分: 将得到的记录集显示到浏览器
set rstest = cnn.execute("select * from 运货商")
do while not rstest.eof    
response.write rstest(0) & "  "  & rstest(1) & "  "  & rstest(2) & "  "  & "
"
    rstest.movenext       
loop

第四部分: 打扫战场
cnn.close
set rstest = nothing: set cnn = nothing
%>


请注意以下几点:
1. 使用sql语句向access数据库中添加数据时必须使用insert into,而向sql server数据库中添加数据,使用insert就能够了。
2. 使用sql语句添加数据的格式如上例,注意需要添加wufeng,在语句中必须使用wufeng,因为sql语句使用作为字符串的分界符。
3. 把本例同以前所学的知识结合起来,就能够实现从html表单中添加数据。
4. 注意有一个数据类型为自动编号的字段,如本例中的“运货商id”,因此您大可不必考虑怎样写代码获得一个递增的编号。

方法二:使用recordset对象的addnew方法,例wuf51.asp。
<% @language = vbscript %>


<% wuf51.asp
第二部分: 使用 recordset 对象的 addnew 方法添加新数据
dim strsql, rstest
set rstest = server.createobject("adodb.recordset")
rstest.cursortype = adopenkeyset  adopendynamic
没有下面这一句, 将不允许更新数据库, 为什么?
rstest.locktype = adlockoptimistic
rstest.open "运货商",cnn,,,adcmdtable

rstest.addnew
rstest("公司名称") = "wufeng"
rstest("电话") = "0571-7227298"
rstest.update
%>


<% 第三部分: 将得到的记录集显示到浏览器
数据库指针移到表中的第一条记录
if not rstest.eof <> 0 then
response.write "表中现有 [" & rstest.recordcount & "] 条数据" & "

"
rstest.movefirst
end if

do while not rstest.eof    
response.write rstest(0) & "  "  & rstest(1) & "  "  & rstest(2) & "  "  & "
"
    rstest.movenext       
loop

第四部分: 打扫战场
cnn.close
set rstest = nothing: set cnn = nothing
%>


分析:
1. 为何要配置rstest.locktype = adlockoptimistic
recordset对象的locktype属性有四个可选值:
adlockreadonly??默认值,表示以只读方式打开记录集,因而无法无法更改数据,在这种情况下使用addnew方法就会发生错误。
adlockpessimistic??保守式记录锁定(逐条)。采用编辑时立即锁定数据源的记录的方式。此时,其他用户不能访问该数据。
adlockoptimistic ??开放式记录锁定(逐条)。只在调用 update 方法时锁定记录。想想,这个属性是不是和我们讲过的application对象的lock、unlock属性的意思差不多。
adlockbatchoptimistic??开放式批更新。用于成批更新数据,和updatebatch方法相对应。
    顺便我们再提一下上一讲中提到的cursortype 属性,他同样有四个值:
adopenforwardonly??仅向前游标,默认值,只能在记录中向前滚动。这能够节省资源并提高性能。
adopenstatic??静态游标。能够用来查找数据或生成报告的记录集合的静态副本。另外,对其他用户所作的添加、更改或删除不可见。 推荐在asp中只使用这两种游标。
adopenkeyset??键集游标。键集游标和动态游标相似,不同的只是禁止查看其他用户添加的记录,并禁止访问其他用户删除的记录,其他用户所作的数据更改将依然可见。
adopendynamic??动态游标。能够看见其他用户所作的添加、更改和删除。允许在记录集中进行任何类型的移动。
能够肯定的一定是,这样抽象的描述有点似是而非,还是弄不太明白,简单的说,
(1) 假如仅仅检索数据,使用默认值就能够了;
(2) 假如使用update方法更新一条数据,locktype属性使用adlockoptimistic,使用updatabatch方法成批更新数据,则使用adlockbatchoptimistic。
(3) 假如对数据库有写动作,cursortype 属性一般使用adopenkeyset就够了。
怎么样?即使还不太明白,但会用了吧。

2. 假如您并不精通数据库,通常在输出显示前使用rstest.movefirst将指针移至第一条记录是大有益处的。但是假如数据库中没有任何数据,就无法使用movefirst方法,所以使用前先用rstest.eof属性判断数据库中是否有数据。
3. 只有当游标类型设为adopenkeyset或adopenstatic时,才能使用recordcount属性(获取记录集中的记录数目)。

二、 修改数据库中已存在的数据
方法一:使用 sql 语句。例wuf52.asp,程式基本上和wuf50.asp类似,这里仅列出关键部分。
第二部分: 使用 connection 对象的 execute 方法修改数据
dim strsql, rstest
strsql = "update 运货商 set 电话 = (503) 555-3188 where 电话 like %99%"
cnn.execute strsql
    修改数据不用insert into…values,而是用update…set语句,where子句的意思是将含有字符串“99”(“like”、“%”在模糊查询时经常用到)的电话号码改为(503) 555-3188,假如不配置条件,表中任何的电话号码都会被改掉。

方法二:使用 recordset 对象的 update 方法。程式wuf53.asp(类似例程wuf51.asp)
第二部分: 使用 recordset 对象的 update 方法修改数据
dim strsql, rstest
set rstest = server.createobject("adodb.recordset")
rstest.locktype = adlockoptimistic
strsql = "select 姓氏,名字,出生日期 from 雇员 where 出生日期 = #55-03-04#"
rstest.open strsql, cnn,,,adcmdtext

rstest("名字") = "中文"
rstest.update
分析:
1. sql语句中,假如数据库是access数据库,则日期用#55-03-04#括起来,如本例;假如是sql server数据库,则日期要用’55-03-04’括起来。
2. rstest.open strsql, cnn,,,adcmdtext中,由于第一个参数是sql语句,所以第五个参数为adcmdtext,其实,第五个参数完万能够省略,但是加上他会使脚本的执行效率更高。
3. 使用方法一,一次能够更新符合条件的任何记录(多条记录或一条记录),但方法二中的update只能修改当前记录(符合条件的第一条记录)。

三、 删除数据库中的数据
方法一:使用 sql 语句。例程wuf55.asp
第二部分: 使用 sql 语句删除数据
dim strsql, rstest
strsql = "delete from 运货商 where 电话 = 0571-7227298"
cnn.execute strsql

方法二:使用 recordset 对象的 delete 方法。例程wuf56.asp
第二部分: 使用 recordset 对象的 delete 方法删除数据
dim strsql, rstest
set rstest = server.createobject("adodb.recordset")
rstest.locktype = adlockoptimistic
strsql = "select * from 运货商 where 电话 = 0571-7227298"
rstest.open strsql, cnn,,,adcmdtext

while not rstest.eof
rstest.delete
rstest.movenext
wend
    若记录集中有多条记录符合条件,则必须使用循环,否则,delete方法只删除当前记录,即第一条符合条件的记录。

四、 其他一些有用的知识
1. 成批更新数据
上面我们讲了怎样使用recordset对象的update方法更新数据,事实上,recordset 对象可支持两类更新:立即更新和批更新。
使用立即更新,一旦调用 update 方法,对数据的任何更改将被立即写入现行数据源。
使用批更新,能够使提供者将多个记录的更改存入缓存,然后使用 updatebatch 方法在单个调用中将他们传送给数据库。更新多个记录时,批更新比立即更新更有效。
缺省为立即更新模式。使用批更新模式,要使用客户端游标,例wuf54.asp。
<% @language = vbscript %>


<% wuf54.asp
第二部分: 批更新模式
dim strsql, rstest
set rstest = server.createobject("adodb.recordset")
rstest.cursorlocation = aduseclient          ‘使用客户端游标类型
rstest.locktype = adlockbatchoptimistic
strsql = "select * from 运货商 where 电话 like %99%"
rstest.open strsql, cnn,,,adcmdtext

rstest.movefirst
while not rstest.eof
rstest("公司名称") = "中文"
rstest.movenext
wend
rstest.updatebatch
%>


<% 第三部分: 将得到的记录集显示到浏览器
rstest.requery
do while not rstest.eof    
response.write rstest(0) & "  "  & rstest(1) & "  "  & rstest(2) & "  "  & "
"
    rstest.movenext       
loop

第四部分: 打扫战场
cnn.close
set rstest = nothing: set cnn = nothing
%>


注意:
1) rstest.cursorlocation = aduseclient有两个值,另一个值为aduseserver(默认),对初学者而言,recordset对象的游标类型是比较难的部分,这里不再周详介绍,以免越来越糊涂,请在实际处理中慢慢摸索(多试)。
2) rstest.requery:使用 requery 方法刷新数据源的 recordset 对象的全部内容。调用该方法等于相继调用 close 和 open 方法。

2.学会使用recordset对象的filter 属性
<% @language = vbscript %>


<% wuf57.asp
第二部分: 使用 recordset 对象的 filter 属性
dim strsql, rstest
set rstest = server.createobject("adodb.recordset")
rstest.cursortype = adopenstatic
rstest.locktype = adlockoptimistic
rstest.open "运货商",cnn,,,adcmdtable

筛选出符合条件的记录,而其他记录则被过滤掉
rstest.filter = "公司名称 = wufeng"
if rstest.eof then       ‘若无此记录,则新增
rstest.addnew
rstest("公司名称") = "wufeng"
rstest("电话") = "0571-7227298"
rstest.update
else           ‘若有符合该条件的记录,修改符合条件的第一条记录
rstest("电话") = "(571) 7227298"
rstest.update
end if
%>


<% 第三部分: 将得到的记录集显示到浏览器
请仔细比较下面这一句要和不要的区分
rstest.filter=""    用来清除 filter 属性
rstest.movefirst
do while not rstest.eof    
response.write rstest(0) & "  "  & rstest(1) & "  "  & rstest(2) & "  "  & "
"
    rstest.movenext       
loop

第四部分: 打扫战场
cnn.close
set rstest = nothing: set cnn = nothing
%>



3.除了上面介绍的两种方法之外,还可使用sql语句、command对象的excute方法维护数据库。例wuf58.asp
<% @language = vbscript %>


<% wuf58.asp
第二部分: 使用sql语句、command对象的excute方法维护数据库
dim strsql, rstest, cmdchange
strsql = "insert into 运货商 (公司名称,电话) values(wufeng,0571-7227298)"
  
创建命令对象。
set cmdchange =server.createobject("adodb.command")
set cmdchange.activeconnection = cnn
cmdchange.commandtext = strsql
cmdchange.execute
%>


<% 第三部分: 将得到的记录集显示到浏览器
set rstest = server.createobject("adodb.recordset")
rstest.open "运货商", cnn, , , adcmdtable
do while not rstest.eof    
response.write rstest(0) & "  "  & rstest(1) & "  "  & rstest(2) & "  "  & "
"
    rstest.movenext       
loop

第四部分: 打扫战场
cnn.close
set rstest = nothing: set cnn = nothing
%>



    本讲主要介绍了维护数据的三种方法,初学者只要掌控前两种方法就能够了。一般而言,尽量使用sql语句解决问题,简单明了;而使用recordset对象的最大好处是能够利用其大量的属性和丰富的游标类型,有更多的选择,但也给使用带来一些难题,关键在于多摸索,多试验。

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅