未提供参数化查询***

时间:2019-03-04 11:04:39

标签: c# database windows-forms-designer id

我遇到错误

  

参数化查询'(@MuscleId int,@ FitnessId nvarchar(4000))SELECT ExerciseId,Naam'期望参数'@FitnessId'未提供。

我尝试用parameter.Value = DBNull.Value修复它。但是我仍然遇到相同的错误,因为我可能做错了。

有人可以告诉我我在做什么错吗?

({MGX.MuscleIdX.FitnessId都设置为int)

代码:

private void Exercises()
{
    string query = "SELECT ExerciseId, Naam FROM Xercises AS X " +
            "INNER JOIN MG_Exercise AS MGX ON MGX.ExerciseId = X.ExerciseId " +
            "WHERE MGX.MuscleId = @MuscleId AND X.FitnessId = @FitnessId";

    using (connection = new SqlConnection(connectionString))
    using (SqlCommand command = new SqlCommand(query, connection))
    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
    {
        SqlParameter param = command.Parameters.AddWithValue("@MuscleId", comBoxMuscleGroup.SelectedValue);
        SqlParameter param2 = command.Parameters.AddWithValue("@FitnessId", comBoxTypeFitness.SelectedValue);

        if (param.Value == null)
        {
            param.Value = DBNull.Value;
        }

        if (param2.Value == null)
        {
            param2.Value = DBNull.Value;
        }

        DataTable Xdata = new DataTable();
        adapter.Fill(Xdata);    *****ERROR*****

        clbXcercises.DisplayMember = "Naam";
        clbXcercises.ValueMember = "ExerciseId";
        clbXcercises.DataSource = Xdata;
    }
}

0 个答案:

没有答案