查看: 657|回复: 3

[文章教程] ASP字典的使用(包含记录集)

[复制链接]
xuanxiao 发表于 2022-3-8 16:09:02 | 显示全部楼层 |阅读模式
  1. Set objDict_Type=Server.CreateObject("Scripting.Dictionary")
  2. sql="select sValue,sName from Dict_Data where [sDict_Type]='Student_Score_Type' order by iSort"
  3. set rs=conn.Execute(sql)
  4.     '//获取统计数据

  5.         while not rs.eof
  6.          
  7.         objDict_Type.Add cstr(rs(1)),rs(0).value
  8.         
  9.         rs.movenext
  10.         wend
  11. rs.close
  12. set rs=nothing

  13. Dim objKeys, objItems, i
  14. objKeys = objDict_Type.Keys
  15. objItems = objDict_Type.Items
  16.    

  17. For i = 0 To objDict_Type.Count - 1
  18.     Response.Write "Key=" & objKeys(i) &_
  19.              " AND Value=" & objItems(i)&vbcrlf
  20. Next

  21. Set objDict_Type=nothing
复制代码


微信截图_20220308160835.png
 楼主| xuanxiao 发表于 2022-4-5 22:09:09 | 显示全部楼层
  1.         Set objDict=Server.CreateObject("Scripting.Dictionary")       
  2.          
  3.          
  4.         objDict.Add cstr(uid),true
  5.               '//select member
  6.                         sql="select id from admin where [iParent] in("&uid&")"
  7.                         set rs=conn.Execute(sql)
  8.                         while not rs.eof
  9.                                    If Not objDict.Exists(cstr(rs(0))) Then
  10.                                  
  11.                          objDict.Add cstr(rs(0)),True
  12.                         End If
  13.                                
  14.                         rs.movenext
  15.                         wend
  16.                         rs.close
  17.                         set rs=nothing
  18.       Get_User_List=Join(objDict.keys,",")
  19.         Set objDict = Nothing
  20.        
  21.         dim key
  22.         for each key in objDict.keys
  23.                 response.write key & " = " & objDict(key)&VBCRLF
  24.         next         
复制代码
 楼主| xuanxiao 发表于 2022-7-11 23:41:30 | 显示全部楼层
新增键值

  1. Dim Dic
  2. Set Dic = CreateObject("Scripting.Dictionary")
  3. '向Dictionary物件中新增键值对
  4. Dic.Add "Name", "Sirrah" 'Add方法第一个参数是Key值,第二个是Item值
  5. Dic.Add "Age", 23 
复制代码

 
刪除键值   

  1. Dim Dic
  2. Set Dic = CreateObject("Scripting.Dictionary")
  3. Dic.Add "Name", "Sirrah" '向Dictionary物件中新增键值对
  4. Dic.Add "Age", 23
  5. Dic.Item("Age") = 22 '修改键Age的值
  6. Print Dic.Item("Age") '输出22
复制代码


判断键是否存在  

  1. Dim Dic
  2. Set Dic = CreateObject("Scripting.Dictionary")
  3. Dic.Add "Name", "Sirrah" '向Dictionary物件中新增键值对
  4. Dic.Add "Age", 23
  5. Print Dic.Exists("Age") '判断键是否存在 
复制代码



 楼主| xuanxiao 发表于 2022-7-11 23:51:58 | 显示全部楼层
一个完整的字典创建管理遍历的实例,包含字典的新增、刪除、判断键是否存在、修改键、修改值、遍历、统计键值对个数
  1. <%
  2. '建立字典
  3. Dim Dict : Set Dict = CreateObject("Scripting.Dictionary")

  4. '新增键值对
  5. Dict.Add "Key1", "Item1"
  6. Dict.Add "Key2", "Item2"
  7. Dict.Add "Key3", "Item3"

  8. '字典中键值对数量
  9. Response.Write "字典中現有键值对数量: " & Dict.Count ' 打印结果

  10. '检查指定键是否存在
  11. If Dict.Exists("Key1") Then
  12. Response.Write "Key1 存在!"
  13. Else
  14. Response.Write "Key1 不存在!"
  15. End If

  16. If Dict.Exists("Keyn") Then
  17. Response.Write "Keyn 存在!"
  18. Else
  19. Response.Write "Keyn 不存在!"
  20. End If

  21. '遍历字典
  22.   Sub TraverseDict
  23.    Dim DictKeys, DictItems, Counter
  24.    DictKeys = Dict.Keys
  25.    DictItems = Dict.Items 'Items返回一个包含所有Item值的阵列
  26.          For Counter = 0 To Dict.Count - 1 'Count返回Dictionary物件键数目
  27.          Response.Write _
  28.           "键: " & DictKeys(Counter) & _ '& 字符串连接
  29.           "值: " & DictItems(Counter)
  30.          Next
  31.   End Sub

  32. TraverseDict


  33. '在一个键值对中,修改键或修改值
  34. Dict.Key("Key2") = "Keyx"
  35. Dict.Item("Key1") = "Itemx"
  36. TraverseDict

  37. '删除指定键
  38. Dict.Remove("Key3")
  39. TraverseDict

  40. '删除全部键
  41. Dict.RemoveAll
  42. Response.Write "字典中现有键值对数量: " & Dict.Count
  43. %>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表

在线客服

售前咨询
售后咨询
服务热线
023-58418553
微信公众号