|
查看: 734|回复: 1
|
急。。random ID的问题。
[复制链接]
|
|
|
我写了一个random ID 的代码。。但是有个小问题出现,当我generate 到一样的ID,它就会出现以下错误:
Microsoft JET Database Engine (0x80040E21)
The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.
我知道这是 怎么一会事,但不懂怎样避免它出现。请大家帮帮忙。。。
其实我要做的是当generate到一样id时,它会自动再generate直到不同id为止,当然,我也不要以上错误出现。。
我的代码:
Randomize()
randNum = (CInt(10 * Rnd) + 1) * +1 'set the 2nd operation to - or+
randNum=trim(randNum)
set conn=server.CreateObject("adodb.connection")
conn1="provider=microsoft.jet.oledb.4.0;data source="&server.MapPath("db/db.mdb")
conn.open conn1
sql1=("select ID from ids where ID='"&randNum&"'")
sql=("select Name from ids where Name='"&name&"'")
set ran=server.createobject("adodb.recordset")
set ran=conn.execute(sql)
if not ran.eof then
response.Write("the record has been added")
response.End
end if
ran.close
set ran=nothing
set rs=server.createobject("adodb.recordset")
sql="select * from ids"
rs.open sql,conn,1,3
rs.addnew
rs("ID")=randNum
rs("Name")=name
rs.update
rs.close
set rs=nothing
response.write("1 record added.")
conn.close
set conn=nothing
请问我应该怎么修改,谢谢。
[ 本帖最后由 geraldthey 于 11-3-2008 05:02 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 13-3-2008 06:16 PM
|
显示全部楼层
大家好,我是佳礼论坛新人,请多多指教。
根据我的理解是:
在你的
sql1=("select ID from ids whereID='"&randNum&"'")
之后,不是应该加一个Loop来鉴定randNum在DB是否已经有了吗?(假设你的ID必须每一个都Unique) 。
Something like :
sql1=("select ID from ids whereID='"&randNum&"'")
set ran=server.createobject("adodb.recordset")
set ran=conn.execute(sql)
While Not ran.EOF
‘Generateanother randNum…
sql1=("selectID from ids where ID='"&randNum&"'")
setran=conn.execute(sql)
Wend
如有不对请多包涵。 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|