【问题标题】:Insert data into table and get ID from increased primary key向表中插入数据并从增加的主键中获取 ID
【发布时间】:2020-05-31 17:44:03
【问题描述】:

在我的 ASP.NET 应用程序中,我需要执行以下操作:首先,从用户那里获取数据,然后将此数据插入 SQL Server 数据库表中,然后从该表中获取 ID 值(即autoincremented),然后在另一个插入另一个表中使用它。

现在,我已经发现为了做到这一点,我需要使用 T-SQL OUTPUT 子句。但是,当我尝试这样做时,会出现此错误:

我尝试遵循这些帖子中的建议: Get output parameter value in ADO.NETUse SQL Server Stored procedure output in asp.net c# 但我无法让它工作。

我没有使用存储过程,而是使用单个 SQL 语句。这里是:

INSERT INTO BS_product_sets (User_id, Set_name, Set_id, Products_count) 
OUTPUT inserted.ID INTO @NEWID 
VALUES (@UID, @SNAME, @SPREFIX, @PCOUNT)

我也试过这个:

DECLARE @NEWID int; 

INSERT INTO BS_product_sets (User_id, Set_name, Set_id, Products_count) 
OUTPUT inserted.ID INTO @NEWID 
VALUES (@UID, @SNAME, @SPREFIX, @PCOUNT)

但后来它变得更加奇怪,因为我得到的错误是:

有没有人知道怎么解决?这是我在 C# 中的其余代码:

public void InsertProductAutoBinds(int user_id, int parameters_count, List<int> cat_ids, List<string> names, List<string> values, string set_name, string set_id)
{
    SqlConnection connection = new SqlConnection(DatabaseConstants.ConnectionString);

    // Adding set to database
    SqlCommand command = new SqlCommand(DatabaseConstants.InsertSet, connection);
    command.Parameters.AddWithValue("@UID", user_id);
    command.Parameters.AddWithValue("@SNAME", set_name);
    command.Parameters.AddWithValue("@SPREFIX", set_id);
    command.Parameters.AddWithValue("@PCOUNT", 0);
    command.Parameters.Add("@NEWID", SqlDbType.Int).Direction = ParameterDirection.Output;

    connection.Open();
    command.ExecuteNonQuery();

    int new_inserted_id = Convert.ToInt32(command.Parameters["@NEWID"].Value);

    connection.Close();

    // Adding binds
    command = new SqlCommand(DatabaseConstants.InsertAutoBinds1, connection);
    command.Parameters.AddWithValue("@UID", user_id);
    command.Parameters.AddWithValue("@SID", new_inserted_id);
    command.Parameters.AddWithValue("@P1ID", cat_ids[0]);
    command.Parameters.AddWithValue("@VN1", names[0]);
    command.Parameters.AddWithValue("@VS1", values[0]);

    if (parameters_count > 1)
    {
        command.CommandText = DatabaseConstants.InsertAutoBinds2;
        command.Parameters.AddWithValue("@P2ID", cat_ids[1]);
        command.Parameters.AddWithValue("@VN2", names[1]);
        command.Parameters.AddWithValue("@VS2", values[1]);
    }

    if (parameters_count > 2)
    {
        command.CommandText = DatabaseConstants.InsertAutoBinds3;
        command.Parameters.AddWithValue("@P3ID", cat_ids[2]);
        command.Parameters.AddWithValue("@VN3", names[2]);
        command.Parameters.AddWithValue("@VS3", values[2]);
    }

    connection.Open();
    command.ExecuteNonQuery();
    connection.Close();
}

【问题讨论】:

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


    【解决方案1】:

    如果您查看official MS documentation for INSERT INTO,您会看到:

    输出到 { @table_variable |输出表}

    这意味着:OUTPUT ... INTO .... 子句仅适用于将表或表变量作为其目标 - 您不能执行 OUTPUT .... INTO @variable - 显然不支持.

    所以你需要

    • 要么执行OUTPUT ... INTO @tableVar,然后从该表变量中选择第一个值并将其返回
    • 只需使用OUTPUT inserted.ID没有任何INTO ....),然后在你的C#代码中获取从SQL命令返回的值——在这种情况下,因为数据 返回时,您需要在 C# 代码中使用 .ExecuteScalar() 而不是 .ExecuteNonQuery()

    【讨论】:

      【解决方案2】:

      如果你有:

      CREATE TABLE tab (Id Int IDENTITY, User_Id Int, ...)
      
      DECLARE @NewID Int
      
      INSERT INTO tab (User_Id,...) VALUES (123, ...)
      
      SET @NewId = @@Identity
      

      【讨论】:

        猜你喜欢
        • 2016-10-08
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 1970-01-01
        • 2016-10-16
        • 1970-01-01
        相关资源
        最近更新 更多