欢迎光临
我们一直在努力

在ASP中用集合成批操作数据库

建站超值云服务器,限时71元/月

我们知道,一般的关系数据库(如sql server、oracle、access等)中的查询操作是支持集合操作的,

例如可以用“update atable set field1 = avalue where field2 in (value21,value22)”来完成对数据

库的成批更新操作。我们可以充分利用数据库的这种集合特性来提高asp页面操作数据库的效率。如我们

可以在页面上列出多个记录,让用户选择要操作的记录,然后在用户确定操作后进行成批操作,这与一个记

录操作一次的方法相比效率明显要高的多了。

一、html的集合属性

  首先,让我们来熟悉一下html的集合属性。在表单(form)数据或查询(query)参数中,当

提交的多个参数采用同一个名称时,这些参数值将构成一个集合,在asp页面可以获取这些参数值或

同名参数的个数。如在下面的页面(set.htm)中,6个复选框采用同一个参数名mycheckbox,其值分别

为1、2、3、4、5、6。

<!– set.htm –>

<html><head><title>集合属性应用</title></head><body>

<p>请选择要操作的项目,提交数据后,将会显示您选择的项目。

<form method="post" action="set.asp">

<br>1、<input type="checkbox" name="mycheckbox" value="1">

<br>2、<input type="checkbox" name="mycheckbox" value="2">

<br>3、<input type="checkbox" name="mycheckbox" value="3">

<br>4、<input type="checkbox" name="mycheckbox" value="4">

<br>5、<input type="checkbox" name="mycheckbox" value="5">

<br>6、<input type="checkbox" name="mycheckbox" value="6">

<br><input type="submit" value="提交数据" name="b1">

</form></body></html>

  当客户端选择了要显示的项目后,下面的asp页面(set.asp)给出客户端选择的项目个数及其值。

<!– set.asp –>

<%@ language = vbscript %>

<html><head><title>集合操作测试</title></head>

<body>

<%

response.write "<br>您一共选择了"&request("mycheckbox").count&"项,"

response.write "<br>您选择的项目有:"&request("mycheckbox")

%>

</body></html>

如当客户端选择了第二、三、五项并提交数据后,将会看到如下结果:

您一共选择了3项,

您选择的项目有:2, 3, 5

应该注意到,“2, 3, 5”的形式与sql语句要求的形式是一致的,我们可以直接或间接地利用这种

形式的结果,如 "select * from atable where afiled in(" & request("mycheckbox") & ")"的实际

sql查询语句为“select * from atable where afiled in(2, 3, 5)”。

二、html的集合属性的应用

  下面我们结合一个实际的例子,讨论一下如何在asp页面中利用html的集合属性来成批操作

数据库。现在我们有一个记录客户电子信箱的access数据库email,其中有一个数据表emaillist,

包含customerid、customername、customeremail三个字段,分别表示客户编号、客户名称、客户电子信箱。

在asp页面selectid.asp中,我们采用checkbox列出所有客户的客户名称(各个checkbox的值为对应的

客户编号),让用户选择给哪些客户发送电子邮件。当用户选择了客户并提交数据后,sendmail.asp将检

索到这些客户的电子信箱,并给这些客户发送电子邮件。具体的信息请参见下面asp程序代码和注释信息。

<!– selectid.asp:列出所有客户的客户名称 –>

<html><head><title>所有客户的客户名称</title></head><body>

<p align=center><font style="font-family:宋体;font-size:9pt">

请选择要给哪些客户发送“新年问候”的电子邮件

<form method="post" action="sendmail.asp">

<%建立与access数据库的连接

set dbconnection = server.createobject("adodb.connection")

dbconnection.open "driver={microsoft access driver (*.mdb)};"&_

"dbq=c:\inetpub\wwwroot\test\email.mdb"

获取所有客户的客户编号、客户名称

set rscustomers = server.createobject("adodb.recordset")

rscustomers.open "select customerid,customername,customeremail from emaillist",_

dbconnection,1,3,1

显示所有客户的客户名称

while not rscustomers.eof

%>

<br><input type="checkbox" name="customerid" value="<%=rscustomers("customerid")%>">

<a href="mailto:<%=rscustomers("customeremail")%>">

<%=rscustomers("customername")%></a>

<%rscustomers.movenext

wend

rscustomers.close

set rscustomers = nothing

dbconnection.close

set dbconnection = nothing

%>

<br><input type="submit" value="给客户发送电子邮件" name="b1"

style="font-family:宋体;font-size:9pt">

</form></body></html>

<!– sendmail.asp:给所选择客户发电子邮件 –>

<html><head><title>给所选择客户发电子邮件</title></head><body>

<p align=center><font style="font-family:宋体;font-size:9pt">

正在给下面客户发送电子邮件

<%建立与access数据库的连接

set dbconnection = server.createobject("adodb.connection")

dbconnection.open "driver={microsoft access driver (*.mdb)};"&_

"dbq=c:\inetpub\wwwroot\test\email.mdb"

获取所选择客户的电子信箱

set rscustomers = server.createobject("adodb.recordset")

rscustomers.open "select customername,customeremail from emaillist where customerid in ("&_

request("customerid")&")",dbconnection,1,3,1

while not rscustomers.eof

给一个客户发电子邮件

set mymail = createobject("cdonts.newmail")

mymail.from = "sales@test.com"

mymail.value("reply-to") = "sales@test.com"

mymail.to = rscustomers("customeremail")

mymail.subject = "来自王发军的新年问候"

mymail.bodyformat = 1

mymail.mailformat = 1

mymail.body = "王发军向"&rscustomers("customername")&"问好!"

mymail.send

set mymail = nothing

%>

<br>给<a href="mailto:<%=rscustomers("customeremail")%>"><%=rscustomers("customername")%></a>

发送电子邮件成功!

<%

rscustomers.movenext

wend

rscustomers.close

set rscustomers = nothing

dbconnection.close

set dbconnection = nothing

%>

<br>在所选择的客户发送电子邮件完毕!

</body></html>

以上程序在winnt4.0+iis4.0+asp2.0+access97下调试通过。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在ASP中用集合成批操作数据库
分享到: 更多 (0)

相关推荐

  • 暂无文章