我要保存一个记录到数据库,又马上需要他的ID号(自动编号的)


 
我不想再去读数据库,而且即便是马上读也不一定能得到,因为其他的字段都是可以有重复的,我该怎么做才能得到呢?  
---------------------------------------------------------------  
 
http://expert.csdn.net/Expert/topic/1443/1443656.xml?temp=.4788629  
---------------------------------------------------------------  
 
如果用addnew方法,在update后直接取rs("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")  
 
---------------------------------------------------------------  
 
并不要求是SQL  SERVER啊。这方法通用  
---------------------------------------------------------------  
 
Function  Get_last_inserted_No()  
           Dim  Rst:Set  Rst=Conn.Execute("Select  @@IDENTITY  As  Id")  
                       If  Not  Rst.Eof  Then  
                                   Get_last_inserted_No=Rst.Fields("Id").value  
                       Else  
                                   Get_last_inserted_No="0"  
                       End  IF  
           Rst.Close  
End  Function
posted on 2005-03-12 23:56  轻松逍遥子  阅读(270)  评论(0)    收藏  举报