【问题标题】:Multiple "If Then Else" in "Do While Not" loop not working, but they do singularly“Do While Not”循环中的多个“If Then Else”不起作用,但它们单独起作用
【发布时间】:2013-02-28 20:29:32
【问题描述】:

好的,这就是我的头发被拉出来的原因!我有一个页面,它从一个 DB 中的表中读取记录,稍微操作数据,然后将其写入另一个 DB。

在 Do While Not EOF 循环中,我有许多 If Then Else 语句来操作数据。

如果我完整地运行代码,大多数 If Then Else 都不起作用,如果我运行页面时只包含每个代码,它们都可以正常工作。

我已经花了 8 个小时以上的时间来解决这个问题,但仍然感到困惑

Dim rsndb, fieldstr1, fieldstr2, fieldstr3, fieldstr4, fieldstr5, fieldstr6, fieldstr7, fieldstr8, fieldstr9, fieldstr10, fieldstr11, fieldstr12, fieldstr13, fieldstr14, fieldstr15
recordstr = 0

'##### Empty temp table #####
Set Connection = Server.CreateObject("ADODB.Connection")
Connection.Open "DSN=website"
cSql = "DELETE FROM newtandltest"
Connection.Execute (cSql)
Connection.Close
Set Connection = Nothing

' Select Records From House Database
Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "DSN=newdatabase;UID=Intranet;Pwd=password;"
Set rsndb = Server.CreateObject("ADODB.Recordset")

strSQL="Select top 500 VendorTransaction.VendorNo As propvendor, Resort.ResortCode As rid, Resort.ResortName As resname, Locations.LocationName As proploc, Country.CountryCode As propcountry, Season.SeasonDesc As propsea, Property.SeasonDesc As propextrasea, Property.Size As psize, Property.Occupancy As occ, FloatingPoints.FloatingPointsName As TAOLF, Property.StartWeek As propsw, Property.EndWeek As propew, Property.Weeks As propweeks, Property.Points As proppoints, Property.WebPricePW As webprice, Property.Priority As priority, Property.OpenToOffer As offers, Property.PoAOffer As poa, Property.RentalPW As rentalpw, Property.WebDate As webdate"

strSQL = strSQL & " From Property Inner Join Resort On Property.ResortId = Resort.Id Inner Join Country On Resort.CountryId = Country.Id Inner Join Locations On Resort.LocationId = Locations.Id Inner Join FloatingPoints On Property.FloatPointId = FloatingPoints.Id Inner Join PropertyStatus On Property.StatusId = PropertyStatus.Id Inner Join PropertyType On Property.TypeId = PropertyType.Id Inner Join Season On Property.SeasonId = Season.Id Inner Join VendorTransaction On VendorTransaction.propertyId = Property.Id Where (Property.WebDate >= DateAdd(day, -187, GetDate()) And Property.StatusId = 4 and year(Property.WebDate) <> 9999)"

rsndb.Open strSQL, adoCon
Set adoCon1 = Server.CreateObject("ADODB.Connection")
adoCon1.Open "DSN=website"

Do While not rsndb.EOF
    '------------------------------------------------------------------------------------------------------------------
    fieldstr1 = rsndb("webdate")
    '------------------------------------------------------------------------------------------------------------------
    fieldstr2 = rsndb("propvendor")
    '------------------------------------------------------------------------------------------------------------------
    fieldstr3 = rsndb("propcountry")
    If rsndb("proploc") = "FLOR" Then
        fieldstr3 = rsndb("proploc")
    End If
    If rsndb("proploc") = "MADE" Then
        fieldstr3 = rsndb("proploc")
    End If
    If rsndb("propcountry") = "ESC" Then
        fieldstr3 = rsndb("propcountry") & left(rsndb("proploc"),1)
    End If
    '------------------------------------------------------------------------------------------------------------------
    resnamestr = replace(rsndb("resname"),chr(13)," ")
    resnamestr = replace(rsndb("resname"),chr(34),"")
    resnamestr = replace(rsndb("resname"),"'","")
    fieldstr4 = resnamestr
    '------------------------------------------------------------------------------------------------------------------
    fieldstr5 = rsndb("propsea")
    If len(rsndb("propextrasea")) > 3 Then
        fieldstr5 = rsndb("propextrasea")
    End If
    If rsndb("propcountry") = "POIN" Then
        fieldstr5 = ""
    End If
    '------------------------------------------------------------------------------------------------------------------
    fieldstr6 = ""
    '------------------------------------------------------------------------------------------------------------------
    fieldstr7 = rsndb("propweeks")
    If rsndb("propcountry") = "POIN" Then
        fieldstr7 = rsndb("proppoints")
    End If
    '------------------------------------------------------------------------------------------------------------------
    If rsndb("propsw") = rsndb("propew") Then
        fieldstr8 = rsndb("propsw")
    End If
    If rsndb("propcountry") = "POIN" Then
        fieldstr8 = "POINTS"
        fieldstr9 = "n/a"
    End If
    If rsndb("propsw") < rsndb("propew") Then
        fieldstr8 = rsndb("propsw") & "/" & rsndb("propew")
    End If
    If rsndb("TAOLF") = "F" Then
        fieldstr8 = "FLOATING"
    End If
    '------------------------------------------------------------------------------------------------------------------
    tempsize = rsndb("psize")
    Select Case tempsize
        Case 1
            fieldstr9 = "Studio" & " - " & rsndb("occ")
        Case 2
            fieldstr9 = "1 Bedroom" & " - " & rsndb("occ")
        Case 3
            fieldstr9 = "2 Bedroom" & " - " & rsndb("occ")
        Case 4
            fieldstr9 = "3 Bedroom" & " - " & rsndb("occ")
        Case 5
            fieldstr9 = "4 Bedroom" & " - " & rsndb("occ")
    End Select
    '------------------------------------------------------------------------------------------------------------------
    fieldstr10 = rsndb("rid")
    '------------------------------------------------------------------------------------------------------------------
    fieldstr11 = rsndb("rid")
    '------------------------------------------------------------------------------------------------------------------
    fieldstr12 = ""
    '------------------------------------------------------------------------------------------------------------------
    fieldstr13 = "N"
    '------------------------------------------------------------------------------------------------------------------
    fieldstr14 = rsndb("webprice")
    If rsndb("offers") = 1 Then
        fieldstr14 = "Offers"
    End If
    If rsndb("poa") = 1 Then
        fieldstr14 = "POA"
    End If
    '------------------------------------------------------------------------------------------------------------------
    fieldstr15 = rsndb("rentalpw")
    lCount = lCount + 1
    Set Connection1 = Server.CreateObject("ADODB.Connection")
    Connection1.Open "DSN=website"
    cSql = "INSERT INTO newtandltest(datefield, field1, field2, field3, field4, field5, field6, field7, field8, field9, field10, field11, field12, price, bargainprice)"
    cSql = cSql & "VALUES('"&fieldstr1&"','"&fieldstr2&"','"&fieldstr3&"','"&fieldstr4&"','"&fieldstr5&"','"&fieldstr6&"','"&fieldstr7&"','"&fieldstr8&"','"&fieldstr9&"','"&fieldstr10&"','"&fieldstr11&"','"&fieldstr12&"','"&fieldstr13&"','"&fieldstr14&"','"&fieldstr15&"');"
    Connection1.Execute (cSql)
    Connection1.Close
    Set Connection1 = Nothing
    rsndb.MoveNext
Loop
rsndb.Close
adoCon.Close
Set rsndb = Nothing
Set adoCon = Nothing

【问题讨论】:

  • 你有没有尝试在这些地方做一些事情来表明程序甚至进入了 if 语句?
  • 是的,但是 If 语句不起作用

标签: asp-classic vbscript


【解决方案1】:

我和影子巫师在一起。整理你的代码,每个键只使用一次 rsndb。并使用字典以合理的名称存储您的字段。像现在这样,它是不可维护的:有人怎么知道 fieldStr12 代表什么?

我发现了一个真正的错误:您对原始字符串执行了 3 次替换,因此实际上只有最后一个替换可见。

建议如何整理你的代码:

Do While not rsndb.EOF

    Set fld = createObject("Scripting.Dictionary")
    fld.Add "webdate",      rsndb("webdate")
    fld.Add "propvendor",   rsndb("propvendor")
    fld.Add "propcountry",  rsndb("propcountry")

    ' etc...

    Select Case fld("proploc")
        Case "FLOR"     fld("propcountry") = fld("proploc")
        Case "MADE"     fld("propcountry") = fld("proploc")
    End case

    If fld("propcountry") = "ESC" Then 
        fld("propcountry") = fld("propcountry") & left(fld("proploc"),1)
    End If

    ' This part is not correct, it is only replacing the ' by an empty string
     'resnamestr = replace(rsndb("resname"),chr(13)," ")
     'resnamestr = replace(rsndb("resname"),chr(34),"")
     'resnamestr = replace(rsndb("resname"),"'","")
     'fieldstr4 = resnamestr

    fld("resname") = replace(fld("resname"), chr(13), " ")
    fld("resname") = replace(fld("resname"), chr(34), "")
    fld("resname") = replace(fld("resname"), "'", "")

    ' etc...

    Set Connection1 = Server.CreateObject("ADODB.Connection")
    Connection1.Open "DSN=website"
    cSql = "INSERT INTO newtandltest(datefield, field1, field2, field3, field4, field5, field6, field7, field8, field9, field10, field11, field12, price, bargainprice)"

    ' the Items property of a dictionary is an array in the same order that you add them to the dictionary
    ' You could do the same with the Keys property.
    cSql = cSql & "VALUES('" & join(fld.Items, "','") & "');"
    Connection1.Execute (cSql)
    Connection1.Close
    Set Connection1 = Nothing
    rsndb.MoveNext
Loop

【讨论】:

  • +1 建议整理代码。仅仅通过保持你的代码有点自我解释,而不是让它看起来像反汇编程序会吐出的东西,你就可以大大增加获得答案的机会。
【解决方案2】:
Something u can improve in your code.

1) You have open connection before while loop so you do not need to open it again in While loop it should be remove 

Set Connection1 = Server.CreateObject("ADODB.Connection")
    Connection1.Open "DSN=website
While loop ...
WEND
Connection1.Close
    Set Connection1 = Nothing

2) check Null and blank before replacing with some value.

for e.g 
if(fld("resname") <> "" AND NOT ISNULL(fld("resname")))
replace(fld("resname"), chr(13), " ")
end if

- Note track loop with default value like response.write ("hello") and check in which row loop is breaking and by which cause

【讨论】:

  • -0.49 - VBScript 中没有 NULL()。你是说 IsNull() 吗?
  • 是的,没错,我的语法不正确,但你可以按照这个逻辑,我已经编辑了那部分代码
  • 是的,我们需要检查 "" 而不是 isnull()
  • 欢迎您,如果您在 ASP 代码方面需要任何其他帮助,请告诉我
【解决方案3】:

我能看到的唯一可能导致这种情况的是多次读取同一个字段;对于某些类型的字段(例如 Access 的备注字段),它可能会在第一次阅读后变为空白。

要解决此类问题,您需要读取每个字段一次,并将其存储到局部变量中:

Do While not rsndb.EOF
    curPropLoc = rsndb("proploc")
    curPropCountry = rsndb("propcountry")
    '...
    fieldstr3 = curPropCountry
    If curPropLoc = "FLOR" Then
        fieldstr3 = curPropLoc
    End If
    If curPropLoc = "MADE" Then
        fieldstr3 = curPropLoc
    End If
    '...keep using curPropLoc instead of rsndb("proploc") throughout the loop...
    '...keep using curPropCountry instead of rsndb("propcountry") throughout the loop...
    '......
    rsndb.MoveNext
Loop

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-26
    • 2018-07-09
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    相关资源
    最近更新 更多