没有从数据库中获取员工列表

时间:2014-09-03 10:41:13

标签: asp.net-mvc asp.net-mvc-4

我正在按部门ID搜索员工。 我想要一个来自数据库的同一部门ID的员工列表。 我无法在存储过程中传递参数。

 SqlCommand cmd = new SqlCommand("SearchEmployeeDetails_SP", sqlConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@dept_id", SqlDbType.BigInt)).Value = enteredDeptId;
reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        EmpList.Add(new EmployeeDetails()
                        {
                            Dept_ID = reader.GetInt32(reader.GetOrdinal("Dept_ID")),
                            Emp_ID = reader.GetInt64(reader.GetOrdinal("Emp_ID")),
                            Hiredate = reader.GetString(reader.GetOrdinal("Hiredate"))
                        });
                    }
                }

1 个答案:

答案 0 :(得分:0)

您尚未将dept_id传递给该过程。希望这会对你有所帮助

 SqlCommand cmd = new SqlCommand("SearchEmployeeDetails_SP", sqlConn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@dept_id", SqlDbType.BigInt)).Value = enteredDeptId;
        cmd.Parameters["@dept_id"].Direction = ParameterDirection.Input;
            reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    EmpList.Add(new EmployeeDetails()
                    {
                        Dept_ID = reader.GetInt32(reader.GetOrdinal("Dept_ID")),
                        Emp_ID = reader.GetInt64(reader.GetOrdinal("Emp_ID")),
                        Hiredate = reader.GetString(reader.GetOrdinal("Hiredate"))
                    });
                }
            }
            reader.Close();
            return EmpList.ToList();
        }