我正在从两个记录集构建ASP经典脚本脚本字典,但是由于某种原因,我尝试添加到字典中的几乎每一项(大约70条)都说该字典存在,而我最终只有3条,这也构建字典后尝试遍历字典时,出现异常。
Dim OrdDict
Set OrdDict = Server.CreateObject("Scripting.Dictionary")
OrdinateSQL = "SELECT COLUMN_NAME, ORDINAL_POSITION FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tblDocTypeTopic';"
Set rsTopicOrdinals = Server.CreateObject("ADODB.RecordSet")
rsTopicOrdinals.Open OrdinateSQL, connUTL, adOpenKeyset
If rsTopicOrdinals.EOF = False Then
Do Until rsTopicOrdinals.EOF
'ordinal positions are "1" indexed, but the array we're building is zero, so subtract one
Response.Write rsTopicOrdinals("COLUMN_NAME") & ": "
If OrdDict.Exists(rsTopicOrdinals("COLUMN_NAME")) Then
response.write "EXISTS<BR>"
OrdDict.Item(rsTopicOrdinals("COLUMN_NAME")) = rsTopicOrdinals("ORDINAL_POSITION") - 1
Else
response.write "NEW<BR>"
OrdDict.Add rsTopicOrdinals("COLUMN_NAME"), rsTopicOrdinals("ORDINAL_POSITION") - 1
End If
rsTopicOrdinals.MoveNext
Loop
End If
rsTopicOrdinals.Close
Set rsTopicOrdinals = Nothing
OrdinateSQL = "SELECT COLUMN_NAME, ORDINAL_POSITION FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tblDocTypeSubTopic';"
Set rsSubTopicOrdinals = Server.CreateObject("ADODB.RecordSet")
rsSubTopicOrdinals.Open OrdinateSQL, connUTL, adOpenKeyset
If rsSubTopicOrdinals.EOF = False Then
Do Until rsSubTopicOrdinals.EOF
'ordinal positions are "1" indexed, add the number of columns in tblDocTypeTopic
'use a unique name for DisplayOrder and Disabled as the exist in both tables
Response.Write rsSubTopicOrdinals("COLUMN_NAME") & ": "
Select Case rsSubTopicOrdinals("COLUMN_NAME")
Case "DisplayOrder"
OrdDict.Add "SubTopicDisplayOrder", rsSubTopicOrdinals("ORDINAL_POSITION") + TopicColCount
Case "Disabled"
OrdDict.Add "SubTopicDisabled", rsSubTopicOrdinals("ORDINAL_POSITION") + TopicColCount
Case "DocTypeTopicID"
Case Else
If OrdDict.Exists(rsSubTopicOrdinals("COLUMN_NAME")) Then
response.write "EXISTS<BR>"
OrdDict.Item(rsSubTopicOrdinals("COLUMN_NAME")) = rsSubTopicOrdinals("ORDINAL_POSITION") + TopicColCount
Else
response.write "NEW<BR>"
OrdDict.Add rsSubTopicOrdinals("COLUMN_NAME"), rsSubTopicOrdinals("ORDINAL_POSITION") + TopicColCount
End If
End Select
rsSubTopicOrdinals.MoveNext
Loop
End If
rsSubTopicOrdinals.Close
Set rsSubTopicOrdinals = Nothing
这是输出:
DocTypeTopicID: NEW
DocTypeID: EXISTS
TopicTitle: EXISTS
DisplayOrder: EXISTS
TopicTitleFontSize: EXISTS
TopicTitleFontFormat: EXISTS
ShortTopicTitle: EXISTS
Disabled: EXISTS
HideTopicOnPublish: EXISTS
DocTypeSubTopicID: NEW
DocTypeTopicID: SubTopicTitle: EXISTS
DisplayOrder: DataType: EXISTS
SubTopicOptions: EXISTS
DefaultValue: EXISTS
Required: EXISTS
Custom: EXISTS
CustomModule: EXISTS
AllowBringForward: EXISTS
AllowForwarding: EXISTS
AllowForwardingChecked: EXISTS
ResponseFontSize: EXISTS
TitleFontSize: EXISTS
TitleFontFormat: EXISTS
OITitle: EXISTS
OITitleFontSize: EXISTS
OITitleFontFormat: EXISTS
OIFontSize: EXISTS
OIFontFormat: EXISTS
OIResponseTitle: EXISTS
OIResponseTitleFontSize: EXISTS
OIResponseTitleFontFormat: EXISTS
OIResponseFontSize: EXISTS
OIResponseFontFormat: EXISTS
OINoResponseDisplay: EXISTS
OIShowModifiedDate: EXISTS
OIShowChangedFlag: EXISTS
OIChangedFlag: EXISTS
OINoInstructionDisplay: EXISTS
DocTypeGridTemplate: EXISTS
DocTypeGridID: EXISTS
GridColumns: EXISTS
GridRows: EXISTS
GridMaxSize: EXISTS
RollupShowSubTopicTitle: EXISTS
TimeBack: EXISTS
SubTopicInt1: EXISTS
SubTopicInt2: EXISTS
SubTopicInt3: EXISTS
SubTopicBit1: EXISTS
SubTopicBit2: EXISTS
SubTopicBit3: EXISTS
SubTopicText1: EXISTS
SubTopicText2: EXISTS
SubTopicText3: EXISTS
AutoPopulateOmit: EXISTS
GridRowType: EXISTS
Disabled: SubTopicInt4: EXISTS
SubTopicInt5: EXISTS
SubTopicInt6: EXISTS
SubTopicBit4: EXISTS
SubTopicBit5: EXISTS
SubTopicBit6: EXISTS
SubTopicText4: EXISTS
SubTopicText5: EXISTS
SubTopicText6: EXISTS
HideSubTopicOnPublish: EXISTS
GridSortColumn: EXISTS
GridSortColumnOrder: EXISTS
TextEditor: EXISTS
CustomListTypeID: EXISTS
CustomListID: EXISTS
当我遍历它时:
OrdKeys = OrdDict.Keys
For i = 0 To OrdDict.Count - 1
Response.Write OrdKeys(i) & ": " & OrdDict.Item(OrdKeys(i))
Next
我得到:
error '80020009'
谁能看到我的代码的缺陷?我只是没有看到明显的东西。
答案 0 :(得分:1)
我以为我已经解决了这个问题,所以我只是删除问题,我认为这很简单……但是万一有人遇到这个问题,这就是我要做的事情
OrdDict.Add CStr(rsTopicOrdinals("COLUMN_NAME")), rsTopicOrdinals("ORDINAL_POSITION") - 1
我只是将字典键包装在CStr中,并且可以正常工作。不管:/
答案 1 :(得分:1)
您面临的问题是您要获取结果的位置,而不是值。因此,当您获取项目时,它会查看RS的位置,但是由于您移至下一个位置,因此该位置将为您提供新记录的值,而不是原始记录的值。我绝对不是专家,但我希望我能理解这一点。
您需要做的事情是,告诉字典添加如下结果的值:
OrdDict.Add rsTopicOrdinals("COLUMN_NAME").value, rsTopicOrdinals("ORDINAL_POSITION").value - 1
添加CStr时发生的是,您根据结果的位置创建了一个新变量,这就是为什么它对您有用的原因。