3

我正在尝试此操作,但ADODB.Recordset error '800a0e78' Operation is not allowed when the object is closed.使用此代码时出现错误If ScopeID.EOF Then

请不要回答使用该CreateParam方法,寻找没有该方法的解决方案。谢谢。

<%  
    set Cmd = Server.CreateObject("ADODB.Command")
    Cmd.ActiveConnection = conn

    Cmd.CommandText = "INSERT INTO TABLE (NAME) VALUES ('test')  SELECT SCOPE_IDENTITY() AS ID"
    Cmd.CommandType = 1
    Cmd.CommandTimeout = 0
    Cmd.Prepared = true

    Set ScopeID = Cmd.Execute()

    If ScopeID.EOF Then
        Response.Write "There was an Error in your request, Please try again"
        Response.End
    Else
        ID= ScopeID(0).Value
    End IF

    ScopeID.Close
    Set ScopeID = Nothing
    Set Cmd = Nothing

    Response.Write ID

%>
4

5 回答 5

5

尝试这个:

Cmd.CommandText = "SET NOCOUNT ON; INSERT INTO TABLE (NAME) VALUES ('test'); SET NOCOUNT OFF; SELECT SCOPE_IDENTITY() AS ID"

据我了解,如果 SET NOCOUNT ON 没有“隐藏”实际插入语句,SQL Server 将返回一个(空)记录集。我认为错误消息是指正在关闭的记录集,而不是连接。

于 2012-04-11T07:14:27.367 回答
3

.NextRecordSet()在命令之后应用就可以了:

<%  
    set Cmd = Server.CreateObject("ADODB.Command")
    Cmd.ActiveConnection = conn

    Cmd.CommandText = "INSERT INTO TABLE (NAME) VALUES ('test')  SELECT SCOPE_IDENTITY() AS ID"
    Cmd.CommandType = 1
    Cmd.CommandTimeout = 0
    Cmd.Prepared = true

    Set ScopeID = Cmd.Execute()
            ScopeID.NextRecordSet() // <---- Fix

    If ScopeID.EOF Then
        Response.Write "There was an Error in your request, Please try again"
        Response.End
    Else
        ID= ScopeID(0).Value
    End IF

    ScopeID.Close
    Set ScopeID = Nothing
    Set Cmd = Nothing

    Response.Write ID

%>
于 2012-04-14T13:56:29.140 回答
1

如果您正在运行现代版本的 SQL Server(我认为是 2005 年),您可以使用输出子句:

Cmd.CommandText = "insert into [dbo].[test] (name) output inserted.id values ('test')"
于 2012-04-16T08:56:22.097 回答
0

将您的查询分为两部分:

INSERT INTO TABLE (NAME) VALUES ('test'); SELECT SCOPE_IDENTITY() AS ID

注意;. 我认为您查询的第二部分没有执行。

于 2012-04-10T08:04:03.900 回答
0

你的连接打开了吗?似乎这就是错误消息所抱怨的内容。您无法针对已关闭的连接执行命令。

于 2012-04-10T02:26:16.480 回答