急!100分,判断是否有重复的值,重复就再生成

xuyadong 2005-05-01 05:23:38
set rs=server.createobject("adodb.recordset")
sql="select * from [ijob_person] where username='"&trim(request("username"))&"' or useremail='"&trim(request("useremail"))&"'"
rs.open sql,conn,1,3
if not rs.eof then
response.write "<br>"+"<li>对不起该用户已经存在!请返回重新输入资料!"
response.end
else
rs.addnew
rs("username")=trim(request("username"))
'---------------------------------------------------
rs("jobid")=gen_key(10)
'---------------------------------------------------

我要一个不重复的JOBID。。。我刚接触ASP,大家帮我啊,急!
...全文
355 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
honghe1 2005-05-10
  • 打赏
  • 举报
回复
Function getNewNumber(length)

seed=0
Const ForReading=1
Const ForWriting=2
Const ForAppending=8
path = server.mappath("./getNewNumber.data") '存放种子的文件
Set objFS = server.CreateObject("Scripting.FileSystemObject")
Set objReadFile = objFS.OpenTextFile(path,ForReading,True)
seed=objReadFile.readLine'读取种
objReadFile.close
Set objWriteFile = objFS.OpenTextFile(path,ForWriting,True)
objWriteFile.writeline (seed+1)'种子自增1写回种子文件做为新的种子
objWriteFile.close

getNewNumber=""
For i=1 to length-Len(seed) step 1
getNewNumber="0" & getNewNumber
Next
getNewNumber=getNewNumber & seed '输出指定长度的自增量
End Function

response.write getNewNumber(10)
'上述代码是为db2这样的数据库没有自动编号类型而写的,希望对大家有用.
comflyer0590 2005-05-10
  • 打赏
  • 举报
回复
错了 qq63423201
comflyer0590 2005-05-10
  • 打赏
  • 举报
回复
qq6343201联系
iasky 2005-05-10
  • 打赏
  • 举报
回复
mark
xuyadong 2005-05-09
  • 打赏
  • 举报
回复
上面的错误行应改为 “do while not rs1.eof” 而不是 ”do while not rs.eof“

这个改过还是不行啊,最后提示超时。。。
mrshelly 2005-05-03
  • 打赏
  • 举报
回复
建议 编号自动生成~~~
xuyadong 2005-05-03
  • 打赏
  • 举报
回复
谢谢大家,问题不解决,我放假都不安心。^_^我现在在家里,电脑破。等下试
Dan1980 2005-05-02
  • 打赏
  • 举报
回复
这种代码效率太低了。

可以修改数据表,给不允许重复的字段添加索引,并设置不允许重复值。

在页面代码中只需要写添加记录的代码,并捕获ADO的错误即可。

VBScript中可以中用On Error Resume Next,先忽略错误,再检索err对象。

JavaScript可以做得更好一些,用类似JAVA风格的try...catch结构,如:

try {
//添加记录
}
catch(e) {
Response.Write("添加数据时出错。<br>");
Response.Write("错误:" + e.description);
}
xuyadong 2005-05-02
  • 打赏
  • 举报
回复
是不是这里错了?
do while not success_flag=true
success_flag=true
random_string=gen_key(10)
rs1.movefirst
xuyadong 2005-05-02
  • 打赏
  • 举报
回复
set rs=server.createobject("adodb.recordset")
sql="select * from [ijob_person] where username='"&trim(request("username"))&"' or useremail='"&trim(request("useremail"))&"'"
rs.open sql,conn,1,3
if not rs.eof then
response.write "<br>"+"<li>对不起该用户已经存在!请返回重新输入资料!"
response.end
else
'-------------------------------------------------
dim rs1,sql1
set rs1=server.createobject("adodb.recordset")
sql1="select * from ijob_person "
rs1.open sql1,conn,1,1

dim success_flag,random_string
success_flag=false
do while not success_flag
success_flag=true
random_string=gen_key(10)
rs1.movefirst
do while not rs.eof
if rs1("jobid")=random_string then
success_flag=false
end if
rs1.movenext
loop
loop
set rs1=nothing
'-------------------------------------------------
rs.addnew
rs("username")=trim(request("username"))
'---------------------------------------------------------
rs("jobid")=random_string
'---------------------------------------------------------
rs("password")=trim(request("password"))
rs("useremail")=trim(request("useremail"))
大哥们,帮我看看,我哪里写错了?怎么还有重复????
xuyadong 2005-05-02
  • 打赏
  • 举报
回复
我也不清楚,我看楼上的代码也没问题啊,但我写上后,还是会出错,是不是我上面哪里写错了?

楼上的大哥帮我看看。

因为这个是注册时查询的,所以效率低也没什么大的影响,用户也不是很多。
comflyer0590 2005-05-02
  • 打赏
  • 举报
回复
不知楼主解决了么? 我写的虽然效率不是很高,但如果数据记录不是很大受的影响不会很大的 为何楼主还会有重复的编号出来呢???
wanghui0380 2005-05-02
  • 打赏
  • 举报
回复
temp=true

a=gen_key(10)
while checkey(a)
a=gen_key(10)
loop


Function Checkkey(a)
set rs1=server.createobject("adodb.recordset")
sql1="select * from table where jobid=" & a //不好意思,刚刚这里写错了,重发一便
rs1.open sql1,conn,1,1
if rs1.eof then temp=false
end function
wanghui0380 2005-05-02
  • 打赏
  • 举报
回复
temp=true

a=gen_key(10)
while checkey(a)
a=gen_key(10)
loop


Function Checkkey(a)
set rs1=server.createobject("adodb.recordset")
sql1="select * from table where jobid=" & gen_key(10)
rs1.open sql1,conn,1,1
if rs1.eof then temp=false
end function
comflyer0590 2005-05-02
  • 打赏
  • 举报
回复
set rs=server.createobject("adodb.recordset")
sql="select * from [ijob_person] where username='"&trim(request("username"))&"' or useremail='"&trim(request("useremail"))&"'"
rs.open sql,conn,1,3
if not rs.eof then
response.write "<br>"+"<li>对不起该用户已经存在!请返回重新输入资料!"
response.end
else
'-------------------------------------------------
dim rs1,sql1
set rs1=server.createobject("adodb.recordset")
sql1="select * from ijob_person "
rs1.open sql1,conn,1,1

dim success_flag,random_string
success_flag=false
do while not success_flag
success_flag=true
random_string=gen_key(10)
rs1.movefirst
do while not rs.eof -----------------------------------‘error
if rs1("jobid")=random_string then
success_flag=false
end if
rs1.movenext
loop
loop
set rs1=nothing
'-------------------------------------------------
rs.addnew
rs("username")=trim(request("username"))
'---------------------------------------------------------
rs("jobid")=random_string
'---------------------------------------------------------
rs("password")=trim(request("password"))
rs("useremail")=trim(request("useremail"))


上面的错误行应改为 “do while not rs1.eof” 而不是 ”do while not rs.eof“
xuyadong 2005-05-02
  • 打赏
  • 举报
回复
你取最后一条数据的ID编号,再加比如100000的数字就行
-阿侠 00:30:13
就是说不会象我这样判断。改那个gen_ken(10)生成的函数?

㊣ ijob ㊣ 00:29:31
是的!
-阿侠 00:32:02
哦,我现在是刚毕业。好多东西不是很懂。
具体点吧。

-阿侠 00:32:57
那这个问题是因为什么产生的?是我们老网数据的转移问题吧

-阿侠 00:36:01
最后一个ID号 47333

㊣ ijob ㊣ 00:37:29
你用数据库读
-阿侠 00:39:16
要总数?

-阿侠 00:45:19
18403行

-阿侠 00:48:40
num = char_array(Int((35 - 0 + 1) * Rnd + 0))
是要改这个吗?
xuyadong 2005-05-02
  • 打赏
  • 举报
回复
这是gen_key 生成的函数
<%
dim output,num
Function gen_key(digits)
dim char_array(50)
char_array(0) = "0"
char_array(1) = "1"
char_array(2) = "2"
char_array(3) = "3"
char_array(4) = "4"
char_array(5) = "5"
char_array(6) = "6"
char_array(7) = "7"
char_array(8) = "8"
char_array(9) = "9"
char_array(10) = "a"
char_array(11) = "b"
char_array(12) = "c"
char_array(13) = "d"
char_array(14) = "e"
char_array(15) = "f"
char_array(16) = "g"
char_array(17) = "h"
char_array(18) = "i"
char_array(19) = "j"
char_array(20) = "k"
char_array(21) = "l"
char_array(22) = "m"
char_array(23) = "n"
char_array(24) = "o"
char_array(25) = "p"
char_array(26) = "q"
char_array(27) = "r"
char_array(28) = "s"
char_array(29) = "t"
char_array(30) = "u"
char_array(31) = "v"
char_array(32) = "w"
char_array(33) = "x"
char_array(34) = "y"
char_array(35) = "z"
randomize
do while len(output) < digits
num = char_array(Int((35 - 0 + 1) * Rnd + 0))
output = output + num
loop

gen_key = output

End Function
%>
xuyadong 2005-05-01
  • 打赏
  • 举报
回复
恩,谢谢!那我就收下了。:)
comflyer0590 2005-05-01
  • 打赏
  • 举报
回复
呵呵 这是我以前做个项目时碰到过类似问题 冷静想了好久弄好的 应该没有问题的!:)
xuyadong 2005-05-01
  • 打赏
  • 举报
回复
上面的通过,不过还不清楚有没有问题。高手们再帮我确定下
加载更多回复(12)

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧