2012-09-27 70 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