public static Contact Load(int contactId) { using (SqlCommand command = new SqlCommand("select * from Contacts where Id = @Id")) { command.Parameters.Add(DataContext.CreateSqlParameter("@Id", contactId)); DataTable dt = DataContext.ExecuteReader(command); if (dt.Rows.Count == 0) { throw new Exception($"Contact with Id {contactId} does not exist."); } Contact contact = new Contact(dt.Rows[0]); contact.LoadEmails(); contact.LoadPhoneNumbers(); return(contact); } }
public static List <Contact> LoadUserContacts(int userId) { DataTable dt; using (SqlCommand command = new SqlCommand("Select * from Contacts where User_id = @User_id")) { command.Parameters.Add(DataContext.CreateSqlParameter("@User_id", userId)); dt = DataContext.ExecuteReader(command); } List <Contact> contacts = new List <Contact>(); foreach (DataRow row in dt.Rows) { Contact contact = new Contact(row); contact.LoadPhoneNumbers(); contact.LoadEmails(); contacts.Add(contact); } return(contacts); }