sql比較複雜有時會用到store procedure
public class EmployeeDataAccessLayer
{
public List<Employee> EmployeeQuery()
{
List<Employee> emp_list = new List<Employee>();
string cs = ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(cs))
{
conn.Open();
SqlCommand cmd = new SqlCommand("EmployeeList", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Employee emp = new Employee();
emp.EmployeeID = Convert.ToInt32(dr["EmployeeID"].ToString());
emp.FirstName = dr["FirstName"].ToString();
emp.City = dr["City"].ToString();
emp_list.Add(emp);
}
return emp_list;
}
}
}