我现在只是在使用Dreamweaver的方法做了这么长时间的作弊之后立即了解插入语句(请不要笑)。
我想弄清楚的一件事是如何获取新插入记录的ID值,以便在成功时我可以将用户重定向到该页面。
我已经看过一些关于存储过程的例子,但是目前它们对我来说是双重的,我还没有了解这些,更不用说如何在我的网页中使用它们了。
摘要
如何使用下面的代码检索用户刚刚插入的记录ID。
工作流
使用ASP页面(add.asp)上显示的HTML表单,用户将提交插入数据库表中的新信息(treebay_transaction)。
在按下提交时,表单数据将传递到另一个页面(add_sql.asp),该页面将提交的数据与其他信息一起提取,并将其插入到所需的表中。
如果插入成功,则需要提取新记录的id值(存储在列treebay_transaction_id
中)以用作查询字符串的一部分,然后用户被重定向到显示新插入的页面记录(view.asp?id = value)。
示例代码 - 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>
答案 0 :(得分:5)
在执行语句之后和关闭连接之前运行此命令:
lsSQL = "SELECT @@IDENTITY AS NewID"
Set loRs = loConn.Execute(lsSQL)
llID = loRs.Fields("NewID").value
我从这里取出它: http://www.kamath.com/tutorials/tut007_identity.asp
答案 1 :(得分: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 :(得分: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注入攻击,请远离串联来自用户的字符串,你应该使用命令参数。
答案 3 :(得分: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
答案 4 :(得分:1)
查看@@ IDENTITY,SCOPE_IDENTITY或IDENT_CURRENT
这假设您的ID字段是IDENTITY INSERT字段。还要考虑列出的各种选项的后果,因为每个选项的行为和执行方式略有不同。
http://sqlserverpedia.com/wiki/Functions_-_@@IDENTITY,_SCOPE_IDENTITY,_IDENT_CURRENT
答案 5 :(得分: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