2

我创建了这个函数,但它似乎给了我一个问题。我想将字典存储到 Session 变量中,以便可以访问整个网站的字典。我不断收到错误Object required: DictionaryObject或者它会说This key already exists in the dictionary。有人可以告诉我我做错了什么吗?

在这篇文章中,我确实看过在会话中存储字典,但并不真正适合我想要做的事情!

Function LoadPermissions()

    Dim SQLString
    SQLString ="SELECT datafields here... FROM " & TBL_employees_permissions & " AS p WHERE p.eid = '" & Clng(12) & "';"

    If IsObject(Session("dicPermissions")) = True Then
        Set dicPermissions = Session("dicPermissions")
    Else
        Set dicPermissions = Server.CreateObject("Scripting.Dictionary")
    End If

    db_conn conn, rs '
    Set myRS = conn.Execute (SQLString) 

    For each item in myRS.Fields
        If IsObject(Session("dicPermissions")) = True AND DictionaryObject.Exists(Trim(item.Name)) = False Then
            dicPermissions.Add Trim(item.Name), Trim(myRS(item.Name))
        End If
    Next
    db_disconn conn, rs

    Set Session("dicPermissions") = dicPermissions 'Store Dictionary to session array.

End Function
4

1 回答 1

2

我能够让它工作,这就是我所做的?如果有人看到任何错误或者我需要添加任何错误捕获。用户登录时加载一次。

暗淡的 SQLString

    SQLString ="SELECT Datefields here... & " AS p WHERE p.eid = '" & Clng(12) & "';"

    'Create the dictionary object.
    Set Session("dicPermissions") = Server.CreateObject("Scripting.Dictionary") 'Create the Dictionary object.

    'sets up a connection to the database
    db_conn conn, rs 'Open account table.
    Set myRS = conn.Execute (SQLString) ' Uses any ADODB connection

    For each item in myRS.Fields 'Create the dictionary with the field names and cell data.
            'dicPermissions.Add fieldname, feild value
            Session("dicPermissions").Add Trim(item.Name), Trim(myRS(item.Name))
    Next
    db_disconn conn, rs 'Close the database

您可以像这样访问它:

Response.write Session("dicPermissions").Item("itemnamehere...")
于 2012-09-03T06:48:22.977 回答