コード例 #1
0
        public IList <Apartment> GetAllApartmentByPostalCode(string code)
        {
            IList <Apartment> apartments = new List <Apartment>();

            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                string sql = "SELECT * FROM APARTMENT ORDER BY POSTALCODE";

                SqlCommand getCommand = new SqlCommand(sql, conn);

                SqlDataReader reader = getCommand.ExecuteReader();

                while (reader.Read())
                {
                    Apartment apartmentElement = ReadApartment(reader);
                    apartments.Add(apartmentElement);
                }
            }
            return(apartments);
        }
コード例 #2
0
        public void UpdateApartment(string id, Apartment newApartment)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                string sql = "UPDATE APARTMENT SET Price = @Price, Location = @Location, PostalCode = @PostalCode, Size = @Size, NoRoom = @NoRoom, WashingMachine = @WashingMachine, Dishwasher = @Dishwasher WHERE Id = @Id";

                SqlCommand updateCommand = new SqlCommand(sql, conn);

                updateCommand.Parameters.AddWithValue("@Id", Int32.Parse(id));

                updateCommand.Parameters.AddWithValue("@Price", newApartment.Price);
                updateCommand.Parameters.AddWithValue("@Location", newApartment.Location);
                updateCommand.Parameters.AddWithValue("@PostalCode", newApartment.PostalCode);
                updateCommand.Parameters.AddWithValue("@Size", newApartment.Size);
                updateCommand.Parameters.AddWithValue("@NoRoom", newApartment.NoRoom);
                updateCommand.Parameters.AddWithValue("@WashingMachine", newApartment.WashingMachine);
                updateCommand.Parameters.AddWithValue("@Dishwasher", newApartment.Dishwasher);

                updateCommand.ExecuteNonQuery();
            }
        }
コード例 #3
0
        //public IList<Apartment> GetPriceRangeApartment(int minprice, int maxprice)
        //{
        //    IList<Apartment> apartments = new List<Apartment>();

        //    using (SqlConnection conn = new SqlConnection(connstr))
        //    {
        //        conn.Open();
        //        string sql = $"SELECT * FROM APARTMENT WHERE PRICE BETWEEN {minprice} AND {maxprice}";

        //        SqlCommand getCommand = new SqlCommand(sql, conn);

        //        SqlDataReader reader = getCommand.ExecuteReader();

        //        while (reader.Read())
        //        {
        //            Apartment apartmentElement = ReadApartment(reader);
        //            apartments.Add(apartmentElement);
        //        }
        //    }
        //    return apartments; ;
        //}

        /// <summary>
        /// Yderligere CRUD metoder:
        /// </summary>
        /// <param name="apartment"></param>
        public void CreateAppartment(Apartment apartment)
        {
            // IList<Apartment> alist = new List<Apartment>();

            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                string sql =
                    "INSERT INTO APARTMENT (Price, Location, PostalCode, Size, NoRoom, WashingMachine, Dishwasher)values(@Price, @Location, @Postalcode, @Size, @NoRoom, @WashingMachine, @Dishwasher)";
                SqlCommand postCommand = new SqlCommand(sql, conn);

                postCommand.Parameters.AddWithValue("@Price", apartment.Price);
                postCommand.Parameters.AddWithValue("@Location", apartment.Location);
                postCommand.Parameters.AddWithValue("@PostalCode", apartment.PostalCode);
                postCommand.Parameters.AddWithValue("@Size", apartment.Size);
                postCommand.Parameters.AddWithValue("@NoRoom", apartment.NoRoom);
                postCommand.Parameters.AddWithValue("@WashingMachine", apartment.WashingMachine);
                postCommand.Parameters.AddWithValue("@Dishwasher", apartment.Dishwasher);

                postCommand.ExecuteNonQuery();
            }
        }