这是一个产生不重复的随机记录的代码,配合数据库使用,需要什么限制就加进来吧Function GetQ()
Dim strsqlQID,rs,StuID
Randomize()
intRandomNumber = Int (1000*Rnd)+1
StuID = Request.Cookies("StudentID")
If StuID = "" Then StuID = Session("StudentID")
If StuID = "" Then
GetQ = -1
Exit Function
End If
strsqlQID = "select top 1 id from Question Where (ModuleID = " & strModuleID & ") And (id Not In (select QID From " & strTableName & " where StuID = " & StuID & ")) Order By  Rnd(" & (-1 * intRandomNumber) & "*id)"
OpenDataBase()
Set rs = cn.Execute(strsqlQID)
If rs.EOF Then
GetQ = -1
Else
GetQ = rs(0)
End If
rs.Close
        CloseDataBase()
End Function
下面是生成不重复的随机数,可以用数组替换Function GetFRandomPwd()
Dim str,pwd,j
pwd = ""
Randomize
str = "0123456789abcdefghijklmnopqrstuvwxyz"
For j = 1 To 8
pwd = pwd & Mid(str,Int((36 * Rnd) + 1),1)
Next
pwdSql = "select id from Student Where StuAnsPwd = '" & pwd & "'"
Dim pwdRs,cnn
Set cnn = Server.CreateObject("ADODB.Connection")
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("xxxxxx.mdb")
Set pwdRs = cn.Execute(pwdSql)
If Not pwdRs.EOF Then
pwdRs.Close
Set pwdRs = Nothing
cnn.Close
Set cnn = Nothing
pwd = GetFRandomPwd()
Else
pwdRs.Close
Set pwdRs = Nothing
cnn.Close
Set cnn = Nothing
End If
GetFRandomPwd = pwd
End Function

解决方案 »

  1.   

    这是一个产生不重复的随机记录的代码,配合数据库使用,需要什么限制就加进来吧Function GetQ()
    Dim strsqlQID,rs,StuID
    Randomize()
    intRandomNumber = Int (1000*Rnd)+1
    StuID = Request.Cookies("StudentID")
    If StuID = "" Then StuID = Session("StudentID")
    If StuID = "" Then
    GetQ = -1
    Exit Function
    End If
    strsqlQID = "select top 1 id from Question Where (ModuleID = " & strModuleID & ") And (id Not In (select QID From " & strTableName & " where StuID = " & StuID & ")) Order By  Rnd(" & (-1 * intRandomNumber) & "*id)"
    OpenDataBase()
    Set rs = cn.Execute(strsqlQID)
    If rs.EOF Then
    GetQ = -1
    Else
    GetQ = rs(0)
    End If
    rs.Close
            CloseDataBase()
    End Function
    下面是生成不重复的随机数,可以用数组替换Function GetFRandomPwd()
    Dim str,pwd,j
    pwd = ""
    Randomize
    str = "0123456789abcdefghijklmnopqrstuvwxyz"
    For j = 1 To 8
    pwd = pwd & Mid(str,Int((36 * Rnd) + 1),1)
    Next
    pwdSql = "select id from Student Where StuAnsPwd = '" & pwd & "'"
    Dim pwdRs,cnn
    Set cnn = Server.CreateObject("ADODB.Connection")
    cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("xxxxxx.mdb")
    Set pwdRs = cn.Execute(pwdSql)
    If Not pwdRs.EOF Then
    pwdRs.Close
    Set pwdRs = Nothing
    cnn.Close
    Set cnn = Nothing
    pwd = GetFRandomPwd()
    Else
    pwdRs.Close
    Set pwdRs = Nothing
    cnn.Close
    Set cnn = Nothing
    End If
    GetFRandomPwd = pwd
    End Function