SqlCommand返回值参数

时间:2012-10-14 19:38:40

标签: c# sql-server ado.net

也许看到这段代码的其他人能够告诉我为什么returnID始终为0.我试图从插入的记录中检索新的ID。

public int AddToInventory(int PartID, int QtyOnHand, int SpokenFor, int LowOrderQty, int HighOrderQty, decimal LastBuyPrice, 
                                    decimal AvgBuyPrice)

        {
        ConfigDAL config = new ConfigDAL();
        string connstr = config.GetConnString();
        SqlConnection conn = new SqlConnection(connstr);

        string query;
        query = "INSERT INTO Inventory (PartID, QtyOnHand, SpokenFor, LowOrderQty, HighOrderQty, LastBuyPrice, "
                    + "AvgBuyPrice, CreatedOn, CreatedBy, ModifiedOn, ModifiedBy) "
                    + "Values (@PartID, @QtyOnHand, @SpokenFor, @LowOrderQty, @HighOrderQty, @LastBuyPrice, @AvgBuyPrice, "
                    + "@CreatedOn, @CreatedBy, @ModifiedOn, @ModifiedBy)";

        SqlCommand cmd = new SqlCommand(query, conn);
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Parameters.AddWithValue("@PartID", PartID);
        cmd.Parameters.AddWithValue("@QtyOnHand", QtyOnHand);
        cmd.Parameters.AddWithValue("@SpokenFor", SpokenFor);
        cmd.Parameters.AddWithValue("@LowOrderQty", LowOrderQty);
        cmd.Parameters.AddWithValue("@HighOrderQty", HighOrderQty);
        cmd.Parameters.AddWithValue("@LastBuyPrice", LastBuyPrice);
        cmd.Parameters.AddWithValue("@AvgBuyPrice", AvgBuyPrice);
        cmd.Parameters.AddWithValue("@CreatedOn", DateTime.Now);
        cmd.Parameters.AddWithValue("@CreatedBy", GlobalProp.UserName);
        cmd.Parameters.AddWithValue("@ModifiedOn", DateTime.Now);
        cmd.Parameters.AddWithValue("@ModifiedBy", GlobalProp.UserName);
        cmd.Parameters.Add("@ID", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        int returnID = (int)cmd.Parameters["@ID"].Value;

        return returnID;
        }

记录会很好地插入到表中,但返回值不正确。我这样做了吗?

由于

3 个答案:

答案 0 :(得分:2)

在SQL查询中再添加一行:

  

选择@ID = scope_identity()

答案 1 :(得分:2)

您没有设置ID的位置,因此可以预期值会发生变化。你必须用

做到这一点
 Select @ID = @Scope_Identity() -- If ID column is an Identity column

OR

Select @ID = @SomeGeneratedValue

试试这个

query = "INSERT INTO Inventory (PartID, QtyOnHand, SpokenFor, LowOrderQty, HighOrderQty, LastBuyPrice, "
                + "AvgBuyPrice, CreatedOn, CreatedBy, ModifiedOn, ModifiedBy) "
                + "Values (@PartID, @QtyOnHand, @SpokenFor, @LowOrderQty, @HighOrderQty, @LastBuyPrice, @AvgBuyPrice, "
                + "@CreatedOn, @CreatedBy, @ModifiedOn, @ModifiedBy);"
                + " Declare @ID int;"
                + " Select @ID = Scope_Identity()";

答案 2 :(得分:1)

根据我的经验 Direction ReturnValue 无效。它有道理,因为在像你这样的命令中,@ ID是一个脚本变量,因此它更像是一个输出参数而不是一个返回值。

对我来说,最好使用 ParameterDirection.Output 而不声明@ID(它是用 .Output 选项声明的):

cmd.Parameters.Add("@ID", SqlDbType.Int).Direction = ParameterDirection.Output;
query = "INSERT INTO Inventory (PartID, QtyOnHand, SpokenFor, LowOrderQty, HighOrderQty, LastBuyPrice, "
            + "AvgBuyPrice, CreatedOn, CreatedBy, ModifiedOn, ModifiedBy) "
            + "Values (@PartID, @QtyOnHand, @SpokenFor, @LowOrderQty, @HighOrderQty, @LastBuyPrice, @AvgBuyPrice, "
            + "@CreatedOn, @CreatedBy, @ModifiedOn, @ModifiedBy);"
            + " Set @ID = Scope_Identity()";