错误:关闭对象时不允许操作

时间:2015-08-06 11:22:55

标签: sql-server vbscript asp-classic

我有以下代码:

    set ors1 = server.CreateObject("ADODB.recordset")

    sqlString = "USE PoSystem OPEN SYMMETRIC KEY WebUsersPasswordEncryption DECRYPTION BY CERTIFICATE EncryptUsersPasswords "&_
                "SELECT UserName,JobTitle,UserRoleID,FullName,EntityID,StatusID,CONVERT(nvarchar(50),DECRYPTBYKEY(Password)) AS  PASSWORD,Email,BeneficiaryID,DATEADD(dd, 0, DATEDIFF(dd, 0, LastLoginDate)) AS LastLoginDate "&_
                "FROM TblWebUsers WHERE UserID="&UserID&" CLOSE SYMMETRIC KEY WebUsersPasswordEncryption"  
    ors1.Open sqlString,conn,3,1

                UserName    = ors1("UserName")
                Remark      = ors1("JobTitle")
                PassWord    = ors1("PassWord")
                Email       = ors1("Email")
                UserRoleID  = ors1("UserRoleID")
                StatusID    = ors1("StatusID") 
                Entity      =  ors1("EntityID")
                userFullName= ors1("FullName")
                BeneficiaryID = ors1("BeneficiaryID")
                LastLoginDate = ors1("LastLoginDate")


     ors1.Close

但我收到以下消息:

Operation is not allowed when the object closed
on ors1("UserName")

我该如何解决?我试图添加SET NOCOUNT ON,但它无效。

2 个答案:

答案 0 :(得分:2)

正如@Paul在评论中提到的,如果您查询没有返回任何记录,那么当您尝试访问行值时,您将收到错误。

通过测试Recordset是否在最后(EOF)来防范此错误。例如:

ors1.Open sqlString,conn,3,1

' Make sure recordset isn't empty...
If Not ors1.EOF Then

    UserName    = ors1("UserName")
    Remark      = ors1("JobTitle")
    PassWord    = ors1("PassWord")
    ...

Else
    ' Query returned no records
End If

如果您需要处理多个记录,请使用循环:

ors1.Open sqlString,conn,3,1

Do Until ors1.EOF
    UserName    = ors1("UserName")
    Remark      = ors1("JobTitle")
    PassWord    = ors1("PassWord")
    ...
    ors1.MoveNext
Loop

答案 1 :(得分:-1)

我发现了我的问题 需要更改连接字符串
从:

ConnStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source='10.1.1.1';Initial Catalog='***';Uid=***;Pwd=*****;"

我将其更改为:

ConnStr="Driver={SQL Server};Server=10.1.1.1;Database=****;Uid=**;Pwd=*****;"