以文本方式查看主题 - ╋艺 镇╋ (http://wdystv.com/bbs/index.asp) -- ┣◇网站建设&Web语言 (http://wdystv.com/bbs/list.asp?boardid=4) ---- 在添加数据时如何获得当前的ID号 (http://wdystv.com/bbs/dispbbs.asp?boardid=4&id=2223) |
-- 作者:admin -- 发布时间:2010/9/4 21:44:55 -- 在添加数据时如何获得当前的ID号 \' Open our table, it \'s important to use the Keyset cursor if \' you \'re using ODBC to connect to your data source, if you \'re \' using OLEDB then you can also use a Dynamic cursor, and \' an Optimistic or Pessimistic Lock, however a Keyset cursor \' with an Optimistic lock in the most resource efficient.. objRst.Open "BurgerChainOwners ", objCnn, adOpenKeyset, _ adLockOptimistic, adCmdTable \' Call the AddNew method, which moves the current row \' pointer on a new row. objRst.AddNew \' Now, set the values for the fields in that row. objRst( "f_name ") = "Ronald " objRst( "s_name ") = "McDonald " objRst( "company ") = "McDonalds Chain of Restaurants " objRst( "fav_burger ") = "Cheeseburger " \' Commit the changes by calling Update. objRst.Update \' Get the value of the record that we just inserted. Response.Write objRst( "id ") ---------- 这个方法其实就是用RS.ADDNEW添加完毕数据后马上获取此ID,在RS没关闭前。 |
-- 作者:admin -- 发布时间:2010/9/4 21:45:30 -- 另一个人提供的三种方法: 这个问题在Access版快已经解决了,包括数据源是access和sqlserver。 方法有3个 如何在表中新插入新记录后,获取该记录自动编号字段的值? 请教一个问题,请各位帮忙!!! Access数据库中,有“自动编号”类型字段,但在表中新插入新记录后,在编程中 如果取得新插入记录的自动编号的值 有 我在写登陆界面的时候也遇到同样的问题 我用 insert into 来添加一个新的人员,但是该人员的password字段值是用这条记录的自动编号字段的值+密码 这样的格式来存储的,也就是说我必须先添加,然后用 update 来设置密码,就遇到了和你相同的问题。 我的办法是:在insert into 的同时将你自己生成的一个uid存储在新纪录的 password 里面,在 update 的时候只要找到这段值就可以了。 以下是我的代码 DoCmd.SetWarnings False Dim strGUID As String strGUID = CreateGUID \'建立一个GUID \'建立GUID有很多方法,你甚至可以建立一个你自己的GUID: 日期+时间+人员姓名+4位数的随机数字 Debug.Print strGUID DoCmd.RunSQL "INSERT INTO tbl_family ( name, pwd ) SELECT \' " & text4.value & " \' AS 表达式1, \' " & strGUID & " \' AS 表达式2 " Dim strUID As String strUID = Trim(str(DLookup( "id ", "tbl_family ", "name= \' " & text4.value & " \' and pwd= \' " & strGUID & " \' "))) DoCmd.RunSQL "UPDATE tbl_family SET tbl_family.pwd = md5( \' " & strUID & "| " & Text6.value & " \') WHERE tbl_family.id= " & strUID DoCmd.SetWarnings True 关于如何得到新增记录的自动增加字段数值方法二 <-ec转移 很简单,先取id后update dim rcd as long Dim rs As New ADODB.Recordset rs.Open "表1 ", CurrentProject.Connection, adOpenDynamic, adLockOptimistic rs.AddNew rcd=rs( "id ") rs.update rs.Close docmd.runsql "update … where id= "&str(rcd) --------- 方法三: Private Sub AutoIncTest() Dim cnn As ADODB.Connection \'Dim cmd As ADODB.Command Dim rst As ADODB.Recordset Set cnn = CurrentProject.Connection \'Set cmd = New ADODB.Command \'Set cmd.ActiveConnection = cnn \'cmd.CommandType = adCmdText \'cmd.CommandText = "INSERT INTO tblNewOrder2 " & _ "(ItemId, Quantity) valueS (1, 20) " \'cmd.Execute \'Set cmd = Nothing cnn.execute "insert into tblneworder2 (item) values ( \'dd \') " Set rst = New ADODB.Recordset rst.Open "SELECT @@IDENTITY AS LastOrderId ", _ cnn, Options:=adCmdText Debug.Print "OrderId for new record = " & _ rst( "LastOrderId ") rst.Close |
-- 作者:admin -- 发布时间:2010/9/4 21:45:51 -- 这个方法未测试: 首先须保证获得记录集的方式支持bookmark属性,如1,3 插入一条带自动编号字段的记录后,获取该记录的bookmark属性值 temp = rs.bookmark 然后 rs.bookmark = temp 试试!! Response.write rs.Fields( "ID ").Value -------------------------------------- 用以上方法可以在Access解决问题 可是在Sqlserver2000中出现以下错误 ADODB.Recordset 错误 \'80040e21 \' 提供者不能确定该值。原因可能是:记录刚刚创建,该字段的默认值不可用,或用户未设置新值。 |
-- 作者:admin -- 发布时间:2010/9/4 21:46:28 -- 另外方法: asp里面,向数据库里面添加一条记录之后,可以立刻获得这个记录的ID号吗?就是自动编号 取得插入 id ... Access 的办法是取得 bookmark 属性 ... yanzi = rs.bookmark SQL Server 里面可以寻找 INDENTITY 的值 ... Select @@IDENTITY AS \'yanzi\' yanzi 就是最后的 id ... 当然你也可以 Select TOP 1 id FROM table orDER BY id DESC .. 只是比较麻烦 ... 首先须保证获得记录集的方式支持bookmark属性,如1,3 插入一条带自动编号字段的记录后,获取该记录的bookmark属性值 temp = rs.bookmark 然后 rs.bookmark = temp 试试!! Response.write rs.Fields("ID").Value 如果用2000以上版本的access,也可以用 Select @@IDENTITY 方法来获得最新ID 我的理解是..添加记录成功后..就输出ID rs.addnew ... rs.update response.write 此记录ID 简单,直接,明了 application.lock() . . . 添加记录 . . . set rs=conn.execute("Select TOP 1 id FROM table orDER BY id DESC") response.write rs("id") application.unlock() rs.addnew ... rs.update id=rs("id") 其实就可以通直接取得id了,在此不能关闭记录集. |
-- 作者:admin -- 发布时间:2010/9/4 21:47:48 -- 附上一个ADDNEW的使用方法 <meta http-equiv="Content-Type" c /> <!--#include file="inc/Conn.asp"--> <!--#include file="Inc/eshopcode.asp"--> <!--#include file="inc/MD5.asp"--> <% If Request.QueryString("action")="" Then Response.Redirect "Index.asp" Else Ip_address=Request.ServerVariables ("HTTP_X_FORWARDED_FOR") If Ip_address="" Then Ip_address= Request.ServerVariables ("REMOTE_ADDR") end if UserName=Trim(Request.Form("UserName")) server_v1=Cstr(Request.ServerVariables("HTTP_REFERER")) if checkstr2(UserName)=true then response.Write(" <center>您输入的用户名中含有\'~!@#$%^&*(),. <>{}[]等特殊字符,请检查重填! <br> <a href=\'"&server_v1&"\'>返回上一页 </a> </center>") response.End() end if Password=Trim(Request.Form("Password")) if checkstr2(Password)=true then response.Write(" <center>您输入的密码中含有\'~!@#$%^&*(),. <>{}[]等特殊字符,请检查重填! <br> <a href=\'"&server_v1&"\'>返回上一页 </a> </center>") response.End() end if Password=MD5(Password,16) usertruepas=trim(request.form("password")) sex=request.form("sex") Question=checkstr(Trim(Request.Form("Question"))) Answer=md5(Trim(Request.Form("Answer")),16) email=checkstr(Trim(Request.Form("email"))) if username="" or password="" then response.Write(" <center>不好意思,您填写的不完整,请后退重填,有问题请联系管理员。 </center>") response.End() end if Set ob=Conn.Execute("Select * From Dv_User Where UserName=\'"&UserName&"\' ") If Not(ob.Eof and ob.Bof) Then Response.write " <script language=\'javascript\'>" & chr(13) Response.write "alert(\'用户名已存在!\');" & Chr(13) Response.write "history.go(-1);"&Chr(13) Response.write " </script>" & Chr(13) Response.End Else Set rs = Server.CreateObject("ADODB.RecordSet") sql = "select * From Dv_User" rs.Open sql,conn,1,3 rs.AddNew rs("UserName")=UserName rs("usersex")=sex rs("UserPassWord")=Password rs("usertruepas")=usertruepas rs("userQuesion")=Question rs("userAnswer")=Answer rs("Useremail")=email if request.QueryString("type")="pers" then rs("usertype")="pers" else if request.QueryString("type")="comp" then rs("usertype")="comp" end if end if Rs("JoinDate")=now() Rs("Lockuser")=0 Rs("Userclass")="新手上路" Rs("UserGroupID")=9 Rs("TitlePic")="level0.gif" Rs("UserFace")="Images/userface/image1.gif" rs("UserIM")="||||||||||||||||||" Rs("UserWidth")=32 Rs("Usertoday")="0|0|0|0|0" Rs("UserHeight")=32 Rs("UserLogins")=1 Rs("LastLogin")=now() Rs("userWealth")=100 Rs("userEP")=60 Rs("usercP")=30 Rs("UserInfo")="||||||||||||||||||||||||||||||||||||||||||" Rs("UserSetting")="1|||0|||0" Rs("UserPower")=0 Rs("UserDel")=0 Rs("UserIsbest")=0 Rs("UserMoney")=0 Rs("UserTicket")=0 Rs("UserFav")="陌生人,我的好友,黑名单" Rs("IsChallenge")=0 Rs("UserHidden")=0 Rs("UserLastIP")=ip_address Rs.Update rs.close set rs=nothing set rs2=server.createobject("adodb.recordset") sql2="select * from dv_setup" rs2.open sql2,conn,1,3 rs2("Forum_UserNum")=rs2("Forum_UserNum")+1 rs2("Forum_lastUser")=username rs2.update rs2.close set rs2=nothing end if if request.QueryString("type")="pers" then Session("PersUserName")=username Session("PersPassWord")=password session("usertype")="pers" Session.Timeout = 30 response.Cookies("37job")("persusername")=username response.Cookies("37job")("perspassword")=password response.cookies("37job").Expires=date+1 Response.write " <script language=\'javascript\'>" & chr(13) Response.write "alert(\'注册成功!请接着填写求职信息\');" & Chr(13) Response.write "window.document.location.href=\'pers_reg3.asp\';"&Chr(13) Response.write " </script>" & Chr(13) Response.End end if if request.QueryString("type")="comp" then Session("CompUserName")=username Session("CompPassWord")=password session("usertype")="comp" session("CompLevel")=0 Session.Timeout = 30 response.Cookies("37job")("compusername")=username response.Cookies("37job")("comppassword")=password response.cookies("37job").Expires=date+1 Response.write " <script language=\'javascript\'>" & chr(13) Response.write "alert(\'注册成功!请接着填写公司信息\');" & Chr(13) Response.write "window.document.location.href=\'comp_reg3.asp\';"&Chr(13) Response.write " </script>" & Chr(13) Response.End end if end if conn.Close set conn=nothing %> |