/// <summary> /// Creates a Campround database object with dummy data /// </summary> /// <param name="name">The name of the campground</param> /// <param name="parkId">The id of the park the campground belongs to</param> /// <returns>A complete database object with dummy data</returns> private CampgroundItem CreateCampground(string name, int parkId) { var campground = new CampgroundItem() { Name = name, ParkId = parkId, OpenFromMonth = 6, OpenToMonth = 11, DailyFee = 100M }; return(campground); }
/// <summary> /// Parses the sql result set into a campground item object /// </summary> /// <param name="reader">The sql data reader that contains the resultset</param> /// <returns>Populated campground item object</returns> private CampgroundItem GetCampgroundItemFromReader(SqlDataReader reader) { CampgroundItem item = new CampgroundItem(); item.Id = Convert.ToInt32(reader["campground_id"]); item.Name = Convert.ToString(reader["name"]); item.ParkId = Convert.ToInt32(reader["park_id"]); item.OpenFromMonth = Convert.ToInt32(reader["open_from_mm"]); item.OpenToMonth = Convert.ToInt32(reader["open_to_mm"]); item.DailyFee = Convert.ToInt32(reader["daily_fee"]); return(item); }
/// <summary> /// Adds a row to the campground table /// </summary> /// <param name="campground">The campground item containing the data to add</param> /// <returns>The autogenerated primary key</returns> public int AddCampground(CampgroundItem campground) { const string sql = "INSERT [campground] (park_id, name, open_from_mm, open_to_mm, daily_fee) " + "VALUES (@park_id, @name, @open_from_mm, @open_to_mm, @daily_fee);"; using (SqlConnection conn = new SqlConnection(_connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql + _getLastIdSQL, conn); cmd.Parameters.AddWithValue("@park_id", campground.ParkId); cmd.Parameters.AddWithValue("@name", campground.Name); cmd.Parameters.AddWithValue("@open_from_mm", campground.OpenFromMonth); cmd.Parameters.AddWithValue("@open_to_mm", campground.OpenToMonth); cmd.Parameters.AddWithValue("@daily_fee", campground.DailyFee); campground.Id = (int)cmd.ExecuteScalar(); } return(campground.Id); }
public CampgroundItem GetCampgroundItem(int Id) { CampgroundItem item = null; const string sql = "Select * From [campground] Where campground_id = @Id;"; using (SqlConnection conn = new SqlConnection(_connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@Id", Id); var reader = cmd.ExecuteReader(); while (reader.Read()) { item = GetCampgroundItemFromReader(reader); } } return(item); }