资讯   |   开发   |   选机中心   |   产品大全 | IBM | 惠普 | 联想 | 戴尔 | 苹果 | 神舟
更多: | 华硕 | 明基 | 方正 | 紫光 | TCL | 夏新 | 联宝 | 宏碁 | 七喜 | 长城 | 清华同方 | 海尔 | 三星 | 东芝 | 索尼 | 富士通 | LG | 技术 | ddnoon
当前位置:笔记本 > 编程开发 >
Advertisement
文章正文

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

类型:转载   责任编辑:asp.net   日期:2007/05/23


热门软件下载:


   
  • asp:如何在Form域中Post大于100K字节的数据?我的方法如下: 
  • 无组件上传图片到数据库中,最完整解决方案 
  • ASP生成Word文档的又一方法 
  • asp中加密与解密对应的函数 
  • 动网论坛密码暴力破解程序代码 
  • IIS又发现10个安全漏洞服务器门户大开 
  • ASP漏洞及安全建议(safefan)(转) 
  • ASP漏洞及安全建议 
  • 一片全面介绍ScriptEncoding加密的文章,很全面可惜是E文的(那位个哥们e文好翻译一下) 
  • 在VB组件内调用Excel2000实现GIF饼图 
  • 页面导航:

    正文内容:
    在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">
     <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下调试通过。
    (作者:王发军 wangfajun@163.net http://wangfajun.163.net)


     

     
    热门推荐笔记本: 宏碁笔记本
    相关文章: