插入后获取新记录的ID

问题描述 投票:1回答:6

我现在只是在使用Dreamweaver的方法做了这么长时间的作弊之后立即了解插入语句(请不要笑)。

我想弄清楚的一件事是如何获取新插入记录的ID值,这样如果成功,我可以将用户重定向到该页面。

我已经看过一些关于存储过程的例子,但是目前它们对我来说是双重的,我还没有了解这些,更不用说如何在我的网页中使用它们了。

摘要

我如何使用下面的代码检索用户刚刚插入的记录ID。

工作流程

使用ASP页面(add.asp)上显示的HTML表单,用户将提交插入数据库表(treebay_transaction)的新信息。

在按下提交时,表单数据将传递到另一个页面(add_sql.asp),该页面将提交的数据与其他信息一起提供,并将其插入到所需的表中。

如果插入成功,则在将用户重定向到显示新插入记录的页面之前,需要提取新记录的id值(存储在treebay_transaction_id列中)以用作查询字符串的一部分(view.asp?id =值)。

示例代码 - add_sql.asp

<!--#include virtual="/Connections/IntranetDB.asp" -->
...
<html>
<body>
<%
set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = MM_IntranetDB_STRING
conn.Open ConnectionString
...
sql="INSERT INTO treebay_transaction (treebay_transaction_seller,treebay_transaction_start_date,treebay_transaction_expiry_date,treebay_transaction_title,treebay_transaction_transaction_type,treebay_transaction_category,treebay_transaction_description,treebay_transaction_status)"
sql=sql & " VALUES "
sql=sql & "('" & CurrentUser & "',"
sql=sql & "'" & timestampCurrent & "',"
sql=sql & "'" & timestampExpiry & "',"
sql=sql & "'" & Request.Form("treebay_transaction_title") & "',"
sql=sql & "'" & Request.Form("treebay_transaction_transaction_type") & "',"
sql=sql & "'" & Request.Form("treebay_transaction_category") & "',"
sql=sql & "'" & Request.Form("xhtml1") & "',"
sql=sql & "'3')"

on error resume next
conn.Execute sql,recaffected
if err<>0 then
%>
<h1>Error!</h1>
<p>
...error text and diagnostics here...
</p>
<%
else
    ' this is where I should be figuring out what the new record ID is
    recordID = ??
    ' the X below represents where that value should be going
    Response.Redirect("index.asp?view.asp?id='" & recordID & "'")
end if
conn.close
%>
</body>
</html>
tsql asp-classic ado
6个回答
5
投票

在执行语句之后和关闭连接之前运行此命令:

lsSQL = "SELECT @@IDENTITY AS NewID"
Set loRs = loConn.Execute(lsSQL)
llID = loRs.Fields("NewID").value

我把它从这里拉出来:http://www.kamath.com/tutorials/tut007_identity.asp


3
投票

像往常一样构建你的sql变量。让我们一次去DB,而不是两次。我们将在与SCOPE_IDENTITY()相同的语句中使用INSERT,以避免多次访问数据库。

在构建SQL语句时添加此项:

sql=sql & "; SELECT SCOPE_IDENTITY() As NewTreebayTransactionID"

'now execute the insert and receive the ID in one Execute statement.
 set newTransactionResults = conn.Execute(sql)

 'here is our new ID.
 recordID = newTransactionResults("NewTreebayTransactionID")

一旦完成:

  • 清理用户的数据输入
  • 使用参数化语句

2
投票

这取决于您使用的数据库,在SQL Server中您可以获取@@ IDENTITY或SCOPE_IDENTITY(),请参阅:http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/

但有一件事我想警告你,上面的代码有严重的安全漏洞,即SQL注入攻击,请远离串联来自用户的字符串,你应该使用命令参数。


2
投票
Set objConn = CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.Recordset")
        objConn.Open "DSN=connectionName"
        rs.CursorLocation = 3
        rs.Open "treebay_transaction", objConn, 3, 3

        rs.AddNew fieldlist,values 'see link bellow to see how to fill this
        rs.Update
        bookmark = rs.absolutePosition  ' First, store the location of you cursor
        rs.Requery                      ' Next, update your recordset with the data from the database
        rs.absolutePosition = bookmark  ' Finally, change your cursor back
        recordID = rs("ID")

rs.AddNew文档:http://www.w3schools.com/ado/met_rs_addnew.asp


1
投票

查看@@ IDENTITY,SCOPE_IDENTITY或IDENT_CURRENT

这假设您的ID字段是IDENTITY INSERT字段。还要考虑列出的各种选项的后果,因为每个选项的行为和执行方式略有不同。

http://sqlserverpedia.com/wiki/Functions_-_@@IDENTITY,_SCOPE_IDENTITY,_IDENT_CURRENT


0
投票

使用相同的交易:

Const adUseServer = 2
Const adOpenKeyset = 1
Const adLockOptimistic = 3

Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open "DSN=connectionName"
  Set oRS = Server.CreateObject("ADODB.RecordSet")
  oRS.CursorLocation = aduseserver
  oRS.CursorType = adopenkeyset
  oRS.LockType = adlockoptimistic
  oRS.Open "treebay_transaction", oConn
  if oRS.eof then
    oRS.AddNew
    oRS("treebay_transaction_seller") = CurrentUser
...
    oRS.Update
    recordID = oRS("treebay_transaction_id")
  end if
  oRS.Close
  set oRS = nothing
oConn.Close
Set oConn = Nothing
© www.soinside.com 2019 - 2024. All rights reserved.