2012-10-14 88 views
3

也許有人在看這個代碼將能夠告訴我爲什麼returnID總是0.我試圖從插入的記錄中檢索新的ID。SqlCommand返回值參數

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; 
     } 

記錄插入到表格中,但返回值不正確。我是否正確地做這件事?

感謝

回答

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()"; 
+0

你的方法告訴我,行cmd.ExecuteNonQuery()運行,我必須聲明標量變量「@ID」 – Ryan

+0

我已更新我的答案'Declare @ID int;' – codingbiz

2

在SQL查詢中增加一個行:

選擇@ID = SCOPE_IDENTITY()

+0

...到底... :-) – Gorgsenegger

1

以我的經驗方向返回值不工作。它有道理,因爲在像你這樣的命令中,@ID是一個腳本變量,所以更多的是輸出參數而不是返回值。

對我來說,最好使用ParameterDirection.Output沒有申報@ID(它被聲明與。輸出選項):

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()";