这里我试图通过Id获取数据请帮助我为什么我的代码写不出来
public IQueryable<Employee> GetEmployee(int id)
{
Employee emp = new Employee();
using (SqlConnection con = db.Getconnection())
{
con.Open();
SqlCommand cmd = new SqlCommand("Sp_GetEmployeeById", con);
cmd.Parameters.AddWithValue("", id);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows == true)
{
while (rdr.Read())
{
emp.Emp_Id = Convert.ToInt32(rdr["Emp_Id"]);
}
return emp;
}
答案 0 :(得分:1)
包括while和return的返回方面。这将解决您的问题。我也没有看到你在方法签名中返回IQueryable的原因。
public Employee GetEmployee(int id)
{
Employee emp = new Employee();
using (SqlConnection con = db.Getconnection())
{
con.Open();
SqlCommand cmd = new SqlCommand("Sp_GetEmployeeById", con);
cmd.Parameters.AddWithValue("", id);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows == true)
{
while (rdr.Read())
{
emp.Emp_Id = Convert.ToInt32(rdr["Emp_Id"]);
emp.EmpName = rdr["EmpName"].ToString();
emp.Email = rdr["Email"].ToString();
emp.Cnt_Id = Convert.ToInt32(rdr["Cnt_Id"]);
}
}
}
return emp;
}