/// <summary> /// Returns the contacts that have last names matching the supplied value. /// </summary> /// <param name="lastName">The last anme to search for in the database.</param> /// <returns>An array of <c>string</c>s that contain the matching contacts. Each entry in the <c>Array</c> contains the contact data in the format: /// "lastName,firstName,phoneNumber".</returns> /// <exception cref="ArgumentException">Thrown if the supplied last name to search for is over length.</exception> public string[] GetEntries(string lastName) { // Validate the supplied value for lastName if (lastName.Length > 50) { throw new ArgumentException("Last name is longer than 50 characters", "lastName"); } // List to hold the returned contacts List <string> entries = new List <string>(); // Create a DataContext and scope it to the database interaction using (PhoneBookDatabaseDataContext dataContext = new PhoneBookDatabaseDataContext()) { // Call the GetEntries stored procedure in the PhoneBook database var results = dataContext.GetEntries(lastName).ToList(); // Loop over the returned contacts and populate the reulsts list, formatting each contact that was found into a single string foreach (PhoneBook entry in results) { entries.Add(formatRecord(entry.LastName) + formatRecord(entry.FirstName) + entry.PhoneNumber.TrimEnd()); } } // Return the results list as an arry of strings return(entries.ToArray()); }
/// <summary> /// Adds a new contact to the database. /// </summary> /// <param name="lastName">The last name of the contact.</param> /// <param name="firstName">The first name of the contact.</param> /// <param name="phoneNumber">The phone number of the contact.</param> /// <returns>A <c>string</c> stating "Contact added" if the contact was successfully added to the database.</returns> /// <exception cref="ArgumentException">Thrown if any of the supplied values is over length.</exception> public string AddEntry(string lastName, string firstName, string phoneNumber) { // Validate the supplied paramters validateContact(lastName, firstName, phoneNumber); // Create a DataContext and scope it to the database interaction using (PhoneBookDatabaseDataContext dataContext = new PhoneBookDatabaseDataContext()) { // Call the AddEntry stored procedure in the database to add the contact dataContext.AddEntry(lastName, firstName, phoneNumber); } // Return a message stating that the contact was added successfully return("Contact added"); }