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

来源:互联网 发布:ps软件官方网站 编辑:程序博客网 时间:2024/06/06 18:27

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

我们知道,一般的关系数据库(如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"> 
  
1、<input type="checkbox" name="MyCheckBox" value="1"> 
  
2、<input type="checkbox" name="MyCheckBox" value="2"> 
  
3、<input type="checkbox" name="MyCheckBox" value="3"> 
  
4、<input type="checkbox" name="MyCheckBox" value="4"> 
  
5、<input type="checkbox" name="MyCheckBox" value="5"> 
  
6、<input type="checkbox" name="MyCheckBox" value="6"> 
  
<input type="submit" value="提交数据" name="B1"> 
</form></body></html> 
   当客户端选择了要显示的项目后,下面的ASP页面(Set.ASP)给出客户端选择的项目个数及其值。 
<!-- Set.ASP --> 
<%@ LANGUAGE = VBScript %> 
<html><head><title>集合操作测试</title></head> 
<body> 
<% 
Response.Write "
您一共选择了"&request("MyCheckBox").count&"项," 
Response.Write "
您选择的项目有:"&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 
%>

<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 
%>

<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 
%>

给<a href="mailto:<%=rsCustomers("CustomerEmail")%>"><% 
=rsCustomers("CustomerName")%></a> 
发送电子邮件成功! 
<% 
rsCustomers.MoveNext 
wend 
rsCustomers.close 
set rsCustomers = nothing 
dbConnection.close 
set dbConnection = nothing 
%>

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

以上程序在WINNT4.0+IIS4.0+ASP2.0+Access97下调试通过。

0 0
原创粉丝点击