2013-05-07 152 views
0

我有一个asp页面出现问题,该页面包含随机函数以显示随机数的随机记录。我现在拥有代码的方式如下所示,即使有几条符合条件的记录,页面上也不会显示任何内容。随机函数无法正常工作

在我的数据库中有大约70条记录匹配条件,但形成某种原因,它们似乎不属于plist变量,用于查询。

当我将最大数字更改为较低数字(例如10)时,我收到一条错误消息,指出已找到EOF或没有更多记录可用。使用调试代码,我找不到任何不寻常的事情,只是从输入到plist的所有100条记录中,没有一条匹配主标准中的记录。

我在这里发布整个代码。也许有人可以捕捉可能导致无法正常工作的原因。

Sub GetRandomDomains 
    dim conn, maxrecords, count, webname 
    dim randomrs, sql 

    'If (conn.State = adStateClosed) Then 
    OpenDB conn 
    'End If 

    count=0 
    maxrecords=100 

    KeywordColumnGetRandomKeywords conn, maxrecords, sql 

    If sql="" then 
     conn.close 
     set conn=nothing 
     Response.Write(" NOT AVAILABLE") 
    exit sub 
    end if 

    set randomrs=conn.execute(sql) 

    Response.Write("<ul id='catnav'>") 
    do While Not randomrs.EOF and count<maxrecords 

     If isnull(randomrs("sitename")) Then 
      webname=randomrs("domain") 
     Else 
      webname=randomrs("sitename") 
     End if 
     Response.Write "<li><a href=""http://www."& randomrs("domain") &"""> &nbsp;" & webname &"</a></li>" 
     count=count+1 
     randomrs.movenext 
    loop 
    Response.Write("</ul>") 

    CloseSet randomrs 
    CloseDB conn 
end sub 

Sub KeywordColumnGetRandomKeywords (conn,maxrecords,sql) 
    dim i, id, index, plist, rs, sqlstr, sortstr 
    plist="" 
    Set rs=Server.CreateObject("ADODB.Recordset") 
    sqlstr="SELECT domainid FROM domains" 
    sqlstr=sqlstr 

    Debugwrite sqlstr, "sqlstr for random domains" 
    rs.Open sqlstr,conn,3,3 
    If rs.eof then 
     CloseSet rs 
     Response.Write(" EMPTY") 
     sql="" 
     exit sub 
    end if 

    Debugwrite rs("domainid"), "rs for random domains" 

    Dim arrData ' Array to Store Data 
    Dim arrSequence ' Array to Hold Random Sequence 
    Dim iArrayLooper ' Integer for Looping 
    Dim iarraysize ' Size of Data Array 

    If xdbasetype="Local" Then 
     iarraysize=cint(rs.recordcount) 
    else 
     iarraysize=cint(GetRecordcount (conn)) 
    end if 

    Debugwrite GetRecordcount(conn), "getrecordcount for random domains array" 

    Debugwrite(IsArray(iarraysize)), "random domains count array" 
    'if (cint(GetRecordcount(conn)))= 0 or isnull(cint(GetRecordcount(conn))) then 
    'Exit Sub 
    'End if 
    redim arrdata(cint(iarraysize)) 
    for i = 0 to iarraysize-1 
     arrData(i)=rs(0) 
     rs.movenext 
    next 
    rs.close 
    Set rs = Nothing 
    If iarraysize<maxrecords then 
     maxrecords=iarraysize 
    end if 
    ' Get an array of numbers 0 to array size randomly sequenced. 
    arrSequence = Resequencearray(iArraySize) 
    for i = 0 to maxrecords-1 
     index=arrsequence(i) 
     id=arrdata(index) 
     if plist<>"" then 
      plist=plist & "," 
     end if 
     plist=plist & id 
    Next 
    sql="select domainid, domain, sitename,sitematch,altmatch from domains" 
    sql = sql & " WHERE restricted=0 and(sitematch like '%" & xsitematch & "%' or altmatch like '%" & xaltmatch & "%')" 
    sql = sql & " and domainid In (" & plist & ") " 
    Debugwrite sql, "first sql for random domains" 
end sub 

Function ResequenceArray(iArraySize) 
    Dim arrTemp() 
    Dim I 
    Dim iLowerBound, iUpperBound 
    Dim iRndNumber 
    Dim iTemp 
    ' Set array size 
    ReDim arrTemp(iArraySize - 1) 
    Randomize 
    iLowerBound = LBound(arrTemp) 
    iUpperBound = UBound(arrTemp) 
    For I = iLowerBound To iUpperBound 
     arrTemp(I) = I 
    Next 
    ' Loop through the array once, swapping each value 
    ' with another in a random location within the array. 
    For I = iLowerBound to iUpperBound 
     iRndNumber = Int(Rnd * (iUpperBound - iLowerBound + 1)) 
     ' Swap Ith element with iRndNumberth element 
     iTemp = arrTemp(I) 
     arrTemp(I) = arrTemp(iRndNumber) 
     arrTemp(iRndNumber) = iTemp 
    Next 'I 
    ' Return our array 
    ResequenceArray = arrTemp 
End Function 

'*********************************************************************** 
' get record count for mysql 
'************************************************************************ 
Function GetrecordCount(conn) 
    dim sqlstr, rs, rcount 
    sqlstr="select count(domainid) FROM domains WHERE restricted=0 and (domaingroup='" & xdomaingroup & "' or altmatch like '%" & xaltmatch & "%')" 

    Debugwrite sqlstr, "sqlstr for random domains" 

    set rs=conn.execute(sqlstr) 
    if rs.eof then 
     rcount=0 
    else 
     rcount=rs(0) 
    end if 
    CloseSet rs 

    Getrecordcount=cint(rcount) 

    Debugwrite rcount, "getrecordcount for random domains" 
End function 
+0

您是否尝试将SQL写入页面并直接在数据库中进行测试? – Paul 2013-05-08 09:56:57

+0

是的,我做了所有三个查询,并单独他们工作正常,但没有办法测试所有三个一起工作,如果我在SQL中运行它,因为它也使用随机数发生器 – Luis 2013-05-10 13:21:42

回答

0

好的。可能有一个更简单的方法来解决这个问题。下面是一段代码,获取数据作为阵列 - 简单得多,我们有过什么在它更多的控制......

'Constants relating to the following routines... 
const C_NO_DATA = "NO_DATA" 'Used when no data is returned to a consuming routine 
const C_ERROR = "ERROR" 'Used when an error is generated 

'GetDataSet 
' Returns a table of data based on the supplied SQL statement and connection string. 
'Parameters: 
' sqlString (string) - The SQL string to be sent. 
' connString (string) - The database connection string. 
'Usage: 
' dataSet = GetDataSet(sqlString, connString) 
'Description: 
' This function generates a table of information in a 2 dimensional array. The first dimension represents the columns 
' and the second the rows. If an error occurs while the routine is executing the array and the base index (0,0) is set 
' to C_ERROR, (0,1) to the VBScript error index, and (0,2) to the VBScript error description. 
function GetDataSet(sqlString, connString) 
    'Initialise... 
    dim returnVal, rsData 
    on error resume next 
     'Define and open the recordset object... 
     set rsData = Server.CreateObject("ADODB.RecordSet") 
     rsData.Open sqlString, connString, 0, 1, 1 
     'Initialise an empty value for the containing array... 
     redim returnVal(0,0) 
     returnVal(0,0) = C_NO_DATA 
     'Deal with any errors... 
     if not rsData.EOF and not rsData.BOF then 
      'Store the data... 
      returnVal = rsData.GetRows() 
      'Tidy up... 
      rsData.close 
      set rsData = nothing 
      select case err.number 
       case 3021 'No data returned 
        'Do nothing as the initial value will still exist (C_NO_DATA) 
       case 0  'No error 
        'Do nothing as data has been returned 
       case else 
        redim returnVal(4,0) 
        returnVal(0,0) = C_ERROR 
        returnVal(1,0) = err.number 
        returnVal(2,0) = err.description 
        returnVal(3,0) = sqlString 
        returnVal(4,0) = connString 
      end select 
     end if 
    on error goto 0 
    'Return the array... 
    GetDataSet = returnVal 
end function 

好了,所以我们看到我们之后是到一个数组中的数据。请注意,我不知道你的xaltmatchxsitematch变量来自所以你需要在某个时候提供这些......

Dim ds, sql 
sql = _ 
    "SELECT " & _ 
    "domainid, " & _ 
    "domain, " & _ 
    "sitename, " & _ 
    "sitematch, " & _ 
    "altmatch " & _ 
"FROM " & _ 
    "domains " & _ 
"WHERE " & _ 
    "restricted=0 AND " & _ 
    "(sitematch LIKE '%" & xsitematch & "%' OR " & _ 
    "altmatch LIKE'%" & xaltmatch & "%') AND " & _ 
    "domainid IN (" & plist & ") " 

ds = GetDataSet(sql, conn) 

ds变量现在包含从数据库中抽取元素的数组。现在我们需要做的是通过所需的次数环......

dim row, rows, used, randoms, col, cols, rc, cc 
rows = UBound(ds, 2) 'Get the upper bound of the second array dimension 
cols = UBound(ds, 1) 'Get the number of columns for writing to the page 
randoms = 70 'Total number of randoms we need 
used = "," 

for rc = 1 to randoms 
    'Just in case we don't want to repeat the same row... 
    do 
     row = int(rnd(1)*rows) 'Zero based - we don't need to add 1 
    until instr(used, "," & row & ",")) = 0 
    'Add our random row to the list... 
    used = used & row & "," 

    'Write our output to the page... 
    response.write("<table>") 
    for cc = 0 to cols 
     response.write("<tr>") 
     response.write("<td>") 
     response.write(ds(cc, row)) 
     response.write("</td>") 
     response.write("</tr>") 
    next 'cc 
    response.write("</table>") 

next 'rc 

GetDataSet代码是我的股票功能的一部分,所以我知道的作品,但我会坚持我的手,说,我还没有测试其余的。

和路易斯打个招呼,让我知道你怎么样。