예제 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to address book problem ADO.net");
            /// Creating an object of AddressBookRepo class
            AddressBookRepo addressBookRepo = new AddressBookRepo();

            //UC1
            addressBookRepo.EnsureDataBaseConnection();
            //UC2
            addressBookRepo.GetAllEntries();
            //UC3
            AddressBookModel model = new AddressBookModel();

            model.FirstName       = "rahul";
            model.LastName        = "vats";
            model.Address         = "fatwua";
            model.City            = "chapra";
            model.State           = "bihar";
            model.Zip             = 281051;
            model.PhoneNo         = 5485200789;
            model.Email           = "*****@*****.**";
            model.AddressBookName = "nikhil";
            model.ContactType     = "Friends";
            Console.WriteLine(addressBookRepo.AddContact(model) ? "Record inserted successfully " : "Failed");

            //UC4
            addressBookRepo.UpdateContact("nikhil");
            //UC5
            addressBookRepo.DeleteContact("neha");
            //UC6
            addressBookRepo.GetPersonByCityOrState();
            //UC7
            ///Calling the GetSizeByCity method
            addressBookRepo.GetSizeByCity("kolkata");
            //UC7-Calling the GetSizeByState method
            addressBookRepo.GetSizeByState("westBengal");
        }
 /// <summary>
 /// UC 3:
 /// Adds the contact.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns></returns>
 /// <exception cref="System.Exception"></exception>
 public bool AddContact(AddressBookModel model)
 {
     try
     {
         using (connection)
         {
             SqlCommand command = new SqlCommand("dbo.SpAddContactRecords", connection);
             command.CommandType = System.Data.CommandType.StoredProcedure;
             command.Parameters.AddWithValue("@fname", model.FirstName);
             command.Parameters.AddWithValue("@sname", model.LastName);
             command.Parameters.AddWithValue("@address", model.Address);
             command.Parameters.AddWithValue("@city", model.City);
             command.Parameters.AddWithValue("@state", model.State);
             command.Parameters.AddWithValue("@zip", model.Zip);
             command.Parameters.AddWithValue("@phoneNo", model.PhoneNo);
             command.Parameters.AddWithValue("@email", model.Email);
             command.Parameters.AddWithValue("@bookName", model.AddressBookName);
             command.Parameters.AddWithValue("@type", model.ContactType);
             connection.Open();
             var result = command.ExecuteNonQuery();
             //connection.Close();
             if (result != 0)
             {
                 return(true);
             }
             return(false);
         }
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message);
     }
     finally
     {
         connection.Close();
     }
 }