2012-10-09 85 views
0

我有一些問題,生成一個唯一的ID,並檢查,以確保生成的ID不在數據庫中。我寫了兩個簡單的函數,但每次運行時,都會出現內存不足錯誤。隨機數字和數據庫檢查

有沒有更好的方法來做到這一點?

這裏是我的代碼:

uniqueID = getID() 

function getID() 
Dim id : id = getUniqueID 'simple random number generator 
    if checkForID(id) = 0 then 
     return id 
    else 
     id = getID() 
    end if 
end function 

function checkForID(id) 
    SQL="SELECT * FROM userOrderDxTest WHERE orderID =" & id 
    set rs=Server.CreateObject("ADODB.recordset") 
    rs.Open SQL,DB,adOpenStatic 
    if not rs.EOF then 
     checkForID = 1 'ID already exists 
     rs.close 
     set rs = nothing 
    else 
     checkForID = 1 'ID doesn't exist 
    rs.CLOSE 
    end if 
end function 

回答

1

您總是在checkForID 1。它應該是:

if not rs.EOF then 
    checkForID = 1 'ID already exists 
    rs.close 
    set rs = nothing 
else 
    checkForID = 0 'ID doesn't exist