/// <summary> /// Updates record in DB with spesified value /// </summary> /// <param name="p">Phone to be updated</param> protected void UpdatePhone(Mock.PhoneBook.Phone p) { if (p != null) { m_sqlconnection.Open(); Dictionary<string, object> dic = new BLL.PhoneConvertor().ToDictionary(p); SqlCommand command = new SqlCommand(UPDATE_PHONE, m_sqlconnection); dic.Where(m => m.Key != "phone_id").ToList().ForEach(o => command.Parameters.AddWithValue("@" + o.Key, o.Value)); if (command.ExecuteNonQuery() == 0) throw new ArgumentException(); m_sqlconnection.Close(); } else throw new ArgumentNullException("Object to insert is null"); }
/// <summary> /// Gets all Persons form DB with JOIN /// </summary> /// <returns>List of Person</returns> public List<Model.Person> GetPersonsJoin() { List<Model.Person> res = new List<Model.Person>(); m_connection.Open(); Infrastructure.PhoneBook.IMockConvertor convertor = new BLL.PersonConvertor(); using (EntityCommand cmd = new EntityCommand(SELECT_PERSONS_ALL, m_connection)) { using (DbDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) { while (reader.Read()) { Dictionary<string, object> dic = Enumerable.Range(0, reader.FieldCount).ToDictionary(reader.GetName, reader.GetValue); Mock.PhoneBook.Person person = new BLL.PersonConvertor().ToObject(dic) as Mock.PhoneBook.Person; if (person.ID == 0) person = Mock.PhoneBook.Person.Null; Mock.PhoneBook.Phone phone = new BLL.PhoneConvertor().ToObject(dic) as Mock.PhoneBook.Phone; if (phone.ID == 0) person = Mock.PhoneBook.Person.Null; Mock.PhoneBook.Address address = new BLL.AddressConvertor().ToObject(dic) as Mock.PhoneBook.Address; if (address.ID == 0) person = Mock.PhoneBook.Person.Null; res.Add(new Model.Person(person, phone, address)); } } } m_connection.Close(); return res; }
/// <summary> /// Sectes all Phones from person table into List /// </summary> /// <returns>List of Phone mocks</returns> protected List<Mock.PhoneBook.Phone> GetPhones() { List<Mock.PhoneBook.Phone> list = new List<Mock.PhoneBook.Phone>(); m_connection.Open(); Infrastructure.PhoneBook.IMockConvertor convertor = new BLL.PhoneConvertor(); using (EntityCommand cmd = new EntityCommand(SELECT_PHONES_ALL, m_connection)) { using (DbDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) { while (reader.Read()) { list.Add(convertor.ToObject(Enumerable.Range(0, reader.FieldCount).ToDictionary(reader.GetName, reader.GetValue)) as Mock.PhoneBook.Phone); } } } m_connection.Close(); return list; }