参数化查询需要未提供的参数

时间:2015-12-27 04:59:05

标签: jquery sql-server json asp.net-mvc

我正在尝试执行附加的代码,我最终得到一个错误。我正在进行ajax调用并将数据发布到控制器方法,我根据收到的值从sql数据库中选择值。请帮助< / p>

控制器代码: -

public ActionResult notCk_Pk(NewOne n)
        {

            List<Counting> l = new List<Counting>();
            String FirstName = n.FirstName;
            String LastName = n.LastName;
            int Salary = n.Salary;
            String Gender = n.Gender;

                string query = "select count(FirstName),count(LastName),Salary,count(Email) from tblEm where FirstName=@FirstName";
                string ConnectionString = ConfigurationManager.ConnectionStrings["EmployeeContext"].ConnectionString;
                SqlDataAdapter da;
                DataSet ds = new DataSet();
                using (SqlConnection connection = new SqlConnection("data source=.; database=Srivatsava; integrated security=SSPI"))
                using (SqlCommand cmd = new SqlCommand(query, connection))
                {
                    cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 50).Value = n.FirstName;
                    connection.Open();
                    cmd.ExecuteNonQuery();
                    connection.Close();
                }
                da = new SqlDataAdapter(query, ConnectionString);
                da.Fill(ds);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    l.Add(new Counting() { FirstNamecount = Convert.ToInt32(dr[0]), LastNamecount = Convert.ToInt32(dr[1]), Salary = Convert.ToInt32(dr[2]), Gendercount = Convert.ToInt32(dr[3]) });
                }



            var todoListsResults = l.Select(
                  a => new
                  {

                      a.FirstNamecount,
                      a.LastNamecount,
                      a.Salary,
                      a.Gendercount

                  });

            var jsonData = new
            {
                //   total = totalPages,
                // page,
                //records = totalRecords,
                rows = todoListsResults
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);




        }

JS: -

$.ajax({
                url: '/TodoList/notCk_Pk',
                data:{'FirstName':FirstName,'LastName':LastName,'Salary':Salary,'Gender':Gender},
                type: "post",
                dataType: 'json',
                success: function (response) {
                    var rows = response.rows;
                    $.each(rows, function () {
                        alert(this.FirstNamecount);
                    });
                }
            })

1 个答案:

答案 0 :(得分:0)

我认为问题在于:

using (SqlCommand cmd = new SqlCommand(query, connection))
                {
                    cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 50).Value = n.FirstName;
                    connection.Open();
                    cmd.ExecuteNonQuery();
                    connection.Close();
                }

试试这个:

using (SqlCommand cmd = new SqlCommand(query, connection))
                {    
                    cmd.Parameters.AddWithValue("@FirstName", n.FirstName);
                    connection.Open();
                    cmd.ExecuteNonQuery();
                    connection.Close();
                }

有关详细信息,请查看SqlCommand.Parameters Property