Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:向循环内的存储过程添加参数_C#_Sql_Stored Procedures_Return_Parameter Passing - Fatal编程技术网

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

C#:向循环内的存储过程添加参数,c#,sql,stored-procedures,return,parameter-passing,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;

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

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

            }

但是,此代码成功运行并显示消息“Added Course successfully!”一次。但是,在第一次成功运行之后,每隔一次运行它就会给我一条“发生错误!可能是数据重复!”的消息。可能的错误是没有清除变量。如何清除以下变量。请帮我做这个。谢谢

重新使用同一个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;
    }
}

此外,我认为正确的说法是,问题在于
您从未从查询中重新提取返回值,您在执行后缺少该行:

尝试使用SqlServer Profiler查看您的代码发送到数据库的内容。无论参数值是否更改,这都将为您提供一个asnwer。您确定数据表
dt
中没有重复的条目吗?为什么要将整数作为varchar传递?