public IEnumerable <PapaDogQuery> GetAll() { using (SqlConnection conn = new SqlConnection()) { List <PapaDogQuery> papas = new List <PapaDogQuery>(); conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "GetAllPapaDogs"; conn.Open(); using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { PapaDogQuery current = new PapaDogQuery(); current.PapaDogId = (int)dr["PapaDogId"]; current.Name = dr["Name"].ToString(); current.Breed = dr["Breed"].ToString(); current.PuppyCount = string.IsNullOrEmpty(dr["PuppyCount"].ToString()) ? 0 : (int)dr["PuppyCount"]; current.LitterCount = string.IsNullOrEmpty(dr["LitterCount"].ToString()) ? 0 : (int)dr["LitterCount"]; current.BirthDate = (DateTime)dr["BirthDate"]; papas.Add(current); } } return(papas); } }
public PapaDogQuery GetById(int id) { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "GetPapaDogById"; cmd.Parameters.AddWithValue("@PapaDogId", id); conn.Open(); using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { PapaDogQuery current = new PapaDogQuery(); current.PapaDogId = (int)dr["PapaDogId"]; current.Name = dr["Name"].ToString(); current.Breed = dr["Breed"].ToString(); current.PuppyCount = string.IsNullOrEmpty(dr["PuppyCount"].ToString()) ? 0 : (int)dr["PuppyCount"]; current.LitterCount = string.IsNullOrEmpty(dr["LitterCount"].ToString()) ? 0 : (int)dr["LitterCount"]; current.BirthDate = (DateTime)dr["BirthDate"]; return(current); } } } return(null); }