求一个asp自增程序!高手帮忙!!

发布时间:2024-05-16 10:52 发布:上海旅游网

问题描述:

要求:向数据库中插入一个数据,要是每次自增1,而且要插入数据要是在最后插入那个最大的数值的基础上自增1
急求!!!高手救命

老大,我想要个程序

<!!---------------------------------------->
sql server 我用的是!!我 想要个这样的asp程序 我不想操作数据库设自增字段

<!-------------------------------->
<!-------------------------------->

<!-------------------------------->

bdstudy 老师你好!! 检查自动生成的编号是否有重复,若有重复则自动加

能不能在给我说明白一些,我是菜鸟....

帮帮忙
<!--------------------->

问题解答:

set rs = server.createobject("adodb.recordset")
'这里ProdId 是数据库里,你要做自增的字段
sql = "select * from bproduc where ProdId is not null"
rs.cursorlocation = 3
rs.open sql,conn,1,1
if rs.bof and rs.eof then'数据库没有数据,则设为1,暂时存到autoid
autoid=1
else
totalid=rs.RecordCount'有数据,根据数据库记录总数增1存到autoid
autoid=totalid+1
end if
rs.close
set rs=nothing

autoidtxt=cstr(autoid)

for i=1 to totalid
'检查自动生成的编号是否有重复,若有重复则自动加1

set rs=conn.execute("select prodid from bproduc where prodid='"&autoidtxt&"'")
if not (rs.eof and rs.bof) then
autoid=autoid+1
autoidtxt=cstr(autoid)
end if
set rs=nothing
next

看一下我这代码,只做关键的简单注释,相信容易看懂。
学程序,要耐心,别懒。重要看动别人写程序的思路,把这种思路固化为自己今后写程序的经验。慢慢积累!

如果你的表名为tablename,字段为ID, 先查一下最大ID是多少,然后加1,插入数据的时候手动插入新ID
rst.open "select max(ID) from tablename",conn,1,1
newid=rst("ID")+1
rst.close
rst.open "tablename",conn,1,3
rst.addnew
rst("ID")=newid
.........

设置关键字段ID,ID设为自增就行了

热点新闻