1

自从我们升级到 SQL Server 2008 后似乎开始出现此错误。

当插入数据库然后返回身份时,我得到一个“在与请求的名称或序号相对应的集合中找不到项目”错误。

这是代码:

SQL = "INSERT INTO PageFeatures(nPageFeatureFlagId,nPageFeatureFeatureId,nPageFeaturePageId) VALUES(" & nTemplateFlagId & "," & nFeatureId & "," & nPageId & "); SELECT SCOPE_IDENTITY() As nPageFeatureId;"
objrs.open SQL,objConn,1,1
    nPageFeatureId = objrs("nPageFeatureId")
objrs.close

插入正在工作,因为记录在数据库中。由于某种原因,它没有返回 id。它工作正常并在 SSMS 中运行时返回 id。但是 ASP 看不到返回的 id,所以有些原因。

任何帮助将不胜感激!

4

2 回答 2

1

您可能必须尝试移动记录集?例如

SQL = "INSERT INTO PageFeatures(nPageFeatureFlagId,nPageFeatureFeatureId,nPageFeaturePageId) VALUES(" & nTemplateFlagId & "," & nFeatureId & "," & nPageId & "); SELECT SCOPE_IDENTITY() As nPageFeatureId;"
objrs.open SQL,objConn,1,1
    objrs.NextRecordset
    nPageFeatureId = objrs("nPageFeatureId")
objrs.close

Raj 关于 SQL 注入的评论仍然是相关的。:)

编辑:详细说明这里有两个记录集。第一个是由插入语句创建的空的,第二个是由分号之后的所有内容引起的;。这是你想要的领域。因此,最初,您的 objrs 附加到其记录集集合中的第一个(而不是记录集中的行),然后移至 NextRecordset - 然后您可以随意处理。

于 2009-10-06T11:41:25.043 回答
1

排序:

SQL = "INSERT INTO PageFeatures(nPageFeatureFlagId,nPageFeatureFeatureId,nPageFeaturePageId) VALUES(" & nTemplateFlagId & "," & nFeatureId & "," & nPageId & ");"
objConn.execute(SQL)

Set oReturnValueRS = objConn.Execute("SELECT SCOPE_IDENTITY()")
nPageFeatureId = oReturnValueRS(0).Value
oReturnValueRS.close : set oReturnValueRS = nothing
于 2009-10-06T14:08:05.983 回答