佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 734|回复: 1

急。。random ID的问题。

[复制链接]
发表于 11-3-2008 04:49 PM | 显示全部楼层 |阅读模式
我写了一个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 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 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

如有不对请多包涵。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 1-1-2026 03:31 PM , Processed in 0.099957 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表