2012-09-27 37 views
0

我想在asp.net中執行存儲過程。存儲過程需要3個參數,所有3個參數都是ID(整數)。這3個參數是:TaskID,ExhibitID和InvestigatorID。用asp.net執行存儲過程

我有一個隱藏字段,其中包含一個來自javascript函數的ExhibitID數組。 我的問題是我如何讓查詢執行,因爲我正在循環數組?

這裏是我的存儲過程的例子:

var cnSaveTask = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ToString()); 
     var comLinkExhibitToTask = new SqlCommand("p_CaseFileTasksExhibitLinkAdd", cnSaveTask) { CommandType = CommandType.StoredProcedure }; 
     foreach (string exhibit in hidExhibitsIDs.Value.Split(',')) 
     { 
      comLinkExhibitToTask.Parameters.AddWithValue("@TaskID", taskID); 
      comLinkExhibitToTask.Parameters.AddWithValue("@ExhibitID", Convert.ToInt32(exhibit)); 
      comLinkExhibitToTask.Parameters.AddWithValue("@InvestigatorID", int.Parse(Session["InvestigatorID"].ToString())); 

     } 

     try 
     { 
      cnSaveTask.Open(); 
      comLinkExhibitToTask.ExecuteNonQuery(); 
     } 

這不是我的工作DB不過。沒有增加。我的猜測是,因爲它正在迭代而不是執行,它每次都會不斷替換「exhibitID」,然後最終嘗試執行它。但我不認爲只是加入"comLinkExhibitToTask.ExecuteNonQuery()" 以外的嘗試是個好主意。有什麼建議麼?

回答

0

解決辦法:

var cnSaveTask = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ToString()); 
     try 
     { 
      var comLinkExhibitToTask = new SqlCommand("p_CaseFileTasksExhibitLinkAdd", cnSaveTask) { CommandType = CommandType.StoredProcedure }; 
      cnSaveTask.Open(); 
      comLinkExhibitToTask.Parameters.Add(new SqlParameter("@TaskID", SqlDbType.Int)); 
      comLinkExhibitToTask.Parameters.Add(new SqlParameter("@ExhibitID", SqlDbType.Int)); 
      comLinkExhibitToTask.Parameters.Add(new SqlParameter("@InvestigatorID", SqlDbType.Int)); 

      foreach (string exhibit in hidExhibitsIDs.Value.Split(',')) 
      { 
       comLinkExhibitToTask.Parameters["@TaskID"].Value = taskID; 
       comLinkExhibitToTask.Parameters["@ExhibitID"].Value = Convert.ToInt32(exhibit); 
       comLinkExhibitToTask.Parameters["@InvestigatorID"].Value = int.Parse(Session["InvestigatorID"].ToString()); 

       comLinkExhibitToTask.ExecuteNonQuery(); 
      } 
     } 
     catch (Exception ex) 
     { 
      ErrorLogger.Log(0, ex.Source, ex.Message); 
     } 
     finally 
     { 
      if (cnSaveTask.State == ConnectionState.Open) 
      { 
       cnSaveTask.Close(); 
      } 
     } 

由於我是在一個循環中它一直添加參數。所以只需在循環外聲明參數,並且只傳遞循環中的值。這樣,只有3個參數,並將值傳入相應

+0

請考慮「使用」語句。它會自動處理你的finally塊。 –

1

你可以將try塊移入​​foreach循環或用try塊包裝foreach循環。 (取決於你希望處理什麼錯誤 - 繼續處理下一個出錯或完全中止執行)​​

1

我從來沒有使用過AddWithValue,所以我不能說它的功能。以下是我通常如何編寫數據庫調用的方法。

using (SqlConnection cnSaveTask = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ConnectionString)) 
{ 
    cnSaveTask.Open(); 

    using (SqlCommand comLinkExhibitToTask = new SqlCommand("p_CaseFileTasksExhibitLinkAdd", cnSaveTask)) 
    { 
     comLinkExhibitToTask.CommandType = CommandType.StoredProcedure; 

     comLinkExhibitToTask.Parameters.Add(new SqlParameter("@TaskID", SqlDbType.Int) {Value = taskID}); 
     // etc. 

     comLinkExhibitToTask.ExecuteNonQuery(); 
    } 
} 
+0

我認爲「AddWithValue」導致的問題,因爲我得到一個「storedProcedureName有太多的參數」。我認爲它只是不斷添加參數。 – user1084319

+0

@ user1084319讓我知道如果這個工程請。 –

+0

會讓你知道幾分鐘。得重新編譯 – user1084319