0

我正在尝试在 asp.net 中执行存储过程。存储过程需要 3 个参数,所有 3 个都是 ID(整数)。这 3 个参数是:TaskID、ExhibitID 和 InvestigatorID。

我有一个隐藏字段,其中包含来自 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();
        }

但它在我的数据库中不起作用。什么都没有添加。我的猜测是,由于它是迭代而不是执行,它只是每次都不断替换“exhibitID”,然后最终尝试执行它。但我不认为只是"comLinkExhibitToTask.ExecuteNonQuery()" 在尝试之外添加是一个好主意。有什么建议么?

4

3 回答 3

1

您可以将 try 块移动到 foreach 循环中,或者用 try 块包装 foreach 循环。(取决于您希望进行的错误处理 - 继续下一个关于错误的展示或完全中止执行)

于 2012-09-27T19:22:28.787 回答
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();
    }
}
于 2012-09-27T19:37:10.883 回答
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个参数,并且值将相应地传入

于 2012-09-27T20:11:33.973 回答