【问题标题】:Can't Get Last Transaction ID in C# ASP.NET在 C# ASP.NET 中无法获取最后一个事务 ID
【发布时间】:2013-02-13 19:56:52
【问题描述】:

我有以下运行存储过程的代码:

    [WebMethod]
    public static string addNewNote(string id, string txt)
    {
        Guid parentId = new Guid(id);
        DbProviderFactory dbf = DbProviderFactories.GetFactory();
        using (IDbConnection con = dbf.CreateConnection())
        {
            con.Open();
            using (IDbTransaction trn = con.BeginTransaction())
            {

                Guid noteId = Guid.Empty;
                SqlProcs.spNOTES_WebForms
                        (ref noteId,
                        parentId,
                        "Files",
                        "Client Note",
                        txt,
                        true
                        );

                IDbCommand cmd = con.CreateCommand();
                cmd.CommandText = "SELECT SCOPE_IDENTITY()";
                cmd.Transaction = trn;

                // Get the last inserted id.
                string insertID = cmd.ExecuteScalar().ToString();
                Debug.Print(insertID.ToString());
            }
        }
        return "";
    }

它不会在输出面板中输出任何内容。如何找回最后一个 ID?

注意:我的 ID 是一个 GUID(唯一标识符)。

编辑:

这是我的存储过程:

if exists (select * from dbo.sysobjects where id = object_id(N'spNOTES_WebForms') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    Drop Procedure dbo.spNOTES_WebForms;
GO

Create Procedure dbo.spNOTES_WebForms
    ( @ID                   uniqueidentifier output
    , @PARENT_ID            uniqueidentifier
    , @PARENT_TYPE          nvarchar(255)
    , @MODIFIED_USER_ID     uniqueidentifier
    , @NAME                 nvarchar(255)
    , @DESCRIPTION          ntext
    , @VISIBLE_TO_CLIENT    bit
    )
with encryption
as
  begin
    set nocount on

    if dbo.fnIsEmptyGuid(@ID) = 1 begin -- then
        set @ID = newid();
    end -- if;
    insert into NOTES
        ( ID               
        , PARENT_ID
        , PARENT_TYPE
        , CREATED_BY       
        , DATE_ENTERED     
        , MODIFIED_USER_ID 
        , DATE_MODIFIED    
        , NAME             
        , DESCRIPTION      
        , VISIBLE_TO_CLIENT
        )
    values
        ( @ID               
        , @PARENT_ID
        , @PARENT_TYPE
        , @MODIFIED_USER_ID 
        ,  getdate()        
        , @MODIFIED_USER_ID 
        ,  getdate()        
        , @NAME             
        , @DESCRIPTION      
        , @VISIBLE_TO_CLIENT
        );

    -- 03/04/2006 Paul.  Add record to custom table. 
    if not exists(select * from NOTES_CSTM where ID_C = @ID) begin -- then
        insert into NOTES_CSTM ( ID_C ) values ( @ID );
    end -- if;

  end
GO

Grant Execute on dbo.spNOTES_WebForms to public;
GO

【问题讨论】:

  • 如果你的“ID”(主键?)是一个 GUID,那么你为什么使用SCOPE_IDENTITY()?查看您的INSERT 代码和相关表结构(CREATE TABLE 脚本)将有助于避免任何混淆。您是否考虑过将存储过程中的新 ID 作为输出参数返回?
  • 我已经考虑过了,但我不知道该怎么做...我在上面的代码中添加了我的存储过程。
  • This question 解释了如何获取 GUID 值; this one 解释了如何使用从 C# 中获取输出参数
  • SCOPE_IDENTITY 返回插入到同一范围内的标识列中的最后一个标识值 -- 范围是一个模块:存储过程、触发器、函数或批处理。所以最好将 SELECT SCOPE_IDENTITY 放入您的存储过程并执行标量。
  • @user1477388 是的。同时执行并返回值。不要把它放在两个不同的命令中。

标签: c# asp.net sql-server webforms


【解决方案1】:

首先你应该检查 isDBNull 与否?而且您的查询没有返回任何 id。 字符串 id = cmd.ExecuteScalar(); 在查询末尾添加SELECT SCOPE_IDENTITY();,然后重试。 Scope_Identity() 也返回一个小数。您可以对查询结果使用 Convert.ToInt32,也可以将返回值转换为十进制然后转换为 int。

【讨论】:

  • 我不确定你想让我做什么。我知道记录正在进入,因为我可以在我的列表中看到它。查询有效,但我似乎无法检索 ID。我的 ID 不是整数或小数,而是GUID。无法检索GUID
  • 您可以像这样将 scope_identifier() 转换为 uniqueidentifier: Guid guid = (Guid)cmd.ExecuteScalar();
  • 它说,“指定的演员阵容无效。”在我跑步时在Guid guid = (Guid)cmd.ExecuteScalar(); 行上。
  • 所以在查询中强制转换为 varchar。 CAST(SCOPE_IDENTITY() AS VARCHAR)
  • 对不起,但它仍然说,“指定的演员表无效。”
猜你喜欢
  • 1970-01-01
  • 2011-05-19
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-01
相关资源
最近更新 更多