C#:将参数添加到循环内的存储过程

时间:2012-10-18 07:40:32

标签: c# sql stored-procedures return parameter-passing

我一直在尝试将参数添加到循环内的存储过程中。下面给出的是我声明变量的代码。

 SqlConnection con = new SqlConnection();
        Connect conn = new Connect();
        SqlDataReader readerCourseID = null;
        con = conn.getConnected();
        con.Open();
        SqlCommand cmdAssignCourse;
        cmdAssignCourse = new SqlCommand("assignCourse", con);
        cmdAssignCourse.CommandType = CommandType.StoredProcedure;
        cmdAssignCourse.Parameters.Add("@sID", System.Data.SqlDbType.VarChar);
        cmdAssignCourse.Parameters.Add("@cID", System.Data.SqlDbType.VarChar);
        SqlParameter retValue = cmdAssignCourse.Parameters.Add("return", System.Data.SqlDbType.Int);

以下是我将值插入先前声明的变量的代码。

foreach (DataRow row in dt.Rows)
            {
                //get course id from course name. Pass  row["Course Name"].ToString()
                int i = getCourseID(row["Course Name"].ToString());



                //assignment of the course to student
                cmdAssignCourse.Parameters["@sID"].Value = studentCurrID.Value.ToString();
                cmdAssignCourse.Parameters["@cID"].Value = i;
                retValue.Direction = ParameterDirection.ReturnValue;
                cmdAssignCourse.ExecuteNonQuery();
                if (retValue.Value.ToString() == "0")
                {
                    MessageBox.Show("Added Course Successfully!");
                    //return 0;
                }
                else
                {
                    MessageBox.Show("An error occured! Possibly a duplication of data!");
                    //return -1;
                }

            }

但是,此代码成功运行并显示消息“已成功添加课程!”一旦。但是在第一次成功运行之后,每次运行它都会给我“发生错误!可能是重复数据!”信息。可能的错误不是清除变量。如何清除以下变量。请帮我解决一下这个。谢谢!

1 个答案:

答案 0 :(得分:1)

通过重用相同的SqlCommand和SqlConnection,您无法获得任何东西。连接池将为您完成所有艰苦的工作,无需重新发明轮子。分离代码会更清晰,更健壮,因此创建一个新方法来执行该过程:

private int GenerateReturnValue(int courseID, int studentID)
{
    using (var connection = new SqlConnection("Your Connection String"))
    using (var command = new SqlCommand("assingCourse", connection)
    {
        connection.Open();
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@sID", System.Data.SqlDbType.VarChar).Value = studentID.ToString();
        command.Parameters.Add("@cID", System.Data.SqlDbType.VarChar).Value = courseID.ToString();
        command.Parameters.Add("@Return", System.Data.SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
        command.ExecuteNonQuery();

        return (int)command.Parameters["@Return"].Value;
    }
}

然后只需在循环中调用该方法。

foreach (DataRow row in dt.Rows)
{
    int i = GenerateReturnValue(getCourseID(row["Course Name"].ToString()), studentCurrID.Value);
    if (i = 0)
    {
        MessageBox.Show("Added Course Successfully!");
        //return 0;
    }
    else
    {
        MessageBox.Show("An error occured! Possibly a duplication of data!");
        //return -1;
    }
}

此外,我认为问题James是正确的,因为问题在于you never re-pull the return value from the query, you are missing that line after execution: