日期:2014-05-17  浏览次数:21221 次

这句Recordset关闭语句怎么错了?
我练习写个asp页面,测试ado的数据库更新操作。
页面的功能是这样的:后台有个数据库userinfo.mdb,有个userinfo表,字段设计是这样
  tvid 文本
  pwd 文本
  logtime 文本
访问asp页的url是这样的:
  http://gdutvrlabnet.h216.000pc.net/test/testSqlUpdate.asp?tvid=12345678&pwd=1234
(以上是个真实地址,大家可以试着访问下帮我测试下)asp页被访问时,从url取参数,检查tvid字段,如发现其值在数据库中没有,就加一个记录。如果发现数据库有了,就检查系统时间,把时间更新写到logtime字段中。

testSqlUpdate.asp代码如下
---------------------------------
HTML code


<%@ Language=VBScript %>
<html>
<head>
    <meta http-equiv="Content-Language" content="zh-cn">
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <title>New Page 1</title>
</head>

<body>
    <p>test ADO 更新语句</p> 
    <%
        tvid = request("tvid")
        pwd = request("pwd")
        if tvid<>"" then
            Set Conn=Server.CreateObject("ADODB.Connection")
            Conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;"&_
                                  "Data Source="& Server.MapPath("userinfo.mdb")
            Conn.Open
            Set RS=Server.CreateObject("ADODB.Recordset")
            sql="select*from userinfo where tvid='"&tvid&"'"
            rs.open sql,conn,1,1
            set rs1=server.createobject("ADODB.Recordset")
            if rs.EOF or rs.bof then
                rs1.Open "userinfo",conn,1,2
                rs1.addnew array("tvid","pwd"), array(tvid,pwd)
                Response.Write "New user added: " + tvid + "(" + pwd + ")"
            else
                DateAndTime = getDate()
                'Response.Write "aaa=" + DateAndTime
                sql1="Update userinfo set logtime=""" + DateAndTime + """ where tvid=""" + tvid + """"
                'Response.Write sql1 
                rs1.open sql1,conn,1,2
                rs1.close        '问题在这句,注释掉这句网页就不出错了。
                set rs1=nothing               
            end if
            rs.close
            set rs=nothing
            set conn=nothing
        end if
    %>
    <%
    function getDate()
        getDate=year(now)&"-"&month(now)&"-"&day(now)&" "&hour(now)&":"&minute(now)&":"&second(now)
    end function
    %>
    </body>
</html>





但以上代码有问题,用新的tvid访问时,数据库增加记录,这时没问题。但用旧的tvid再访问一次时,就出错了。提示:
  ADODB.Recordset 错误 '800a0e78' 
  对象关闭时,不允许操作。

我检查到问题在“rs1.close”这句,注释掉这句网页功能就正常了,不出错了。
但我百思不得其解,这句怎么会有问题呢?难道记录集不需要关闭吗?另一个记录集变量rs不也正常close掉了吗?要是我就让rs1在网页中不关闭,就这样发布到服务器上,会不会有什么后果呢?

------解决方案--------------------
打开成功了没有?
------解决方案--------------------
另外,update语句是不返回recordset的。直接运行就可以了。
------解决方案--------------------
VBScript code

               sql1 = "Update userinfo set logtime='" & DateAndTime & "' where tvid='" & tvid & "'"
               conn.execute sql1

------解决方案--------------------
C# code
[color=#FF0000]sql="select*from userinfo where tvid='"&tvid&"'"[/color]