public bool Addcontatct(AddressBookModel data) { SqlConnection connection = new SqlConnection(connectionString); try { using (connection) { SqlCommand command = new SqlCommand("Sp_AddContactDetails", connection); command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("@first_name", data.first_name); command.Parameters.AddWithValue("@last_name", data.last_name); command.Parameters.AddWithValue("@address", data.address); command.Parameters.AddWithValue("@city", data.city); command.Parameters.AddWithValue("@state", data.state); command.Parameters.AddWithValue("@zip", data.zip); command.Parameters.AddWithValue("@phone_number", data.phone_number); command.Parameters.AddWithValue("@email", data.email); command.Parameters.AddWithValue("@addressbook_name", data.addressbook_name); command.Parameters.AddWithValue("@addressbook_type", data.addressbook_type); connection.Open(); var result = command.ExecuteNonQuery(); connection.Close(); if (result != 0) { return(true); } return(false); } } catch (Exception e) { throw new Exception(e.Message); } finally { connection.Close(); Console.WriteLine("connection close"); } }
public void SortPersonNameByCity() { SqlConnection connection = new SqlConnection(connectionString); try { AddressBookModel model = new AddressBookModel(); using (connection) { using (SqlCommand command = new SqlCommand( @"SELECT * FROM AddreshBookADo WHERE city = 'Rishikesh' order by first_name; SELECT * FROM AddreshBookADo WHERE city = 'Ruderpryag' order by first_name, last_name;", connection)) { connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { Console.WriteLine("------Sorted Contact based of first name of person belonging to city Rishikesh-----"); while (reader.Read()) { model.first_name = reader.GetString(0); model.last_name = reader.GetString(1); model.address = reader.GetString(2); model.city = reader.GetString(3); model.state = reader.GetString(4); model.zip = reader.GetInt32(5); model.phone_number = reader.GetString(6); model.email = reader.GetString(7); Console.WriteLine($"\nFirrst Nmae::{model.first_name}\nlast name::{model.last_name}\naddress::{model.address}\ncity::{model.city}\nstate::{model.state}\nzip::{model.zip}\nphone_number::{model.phone_number}\nemail::{model.email}"); Console.WriteLine("------------\n"); } if (reader.NextResult()) { Console.WriteLine("------Sorted Contact based of first name of person belonging to city Ruderpryag-----"); while (reader.Read()) { model.first_name = reader.GetString(0); model.last_name = reader.GetString(1); model.address = reader.GetString(2); model.city = reader.GetString(3); model.state = reader.GetString(4); model.zip = reader.GetInt32(5); model.phone_number = reader.GetString(6); model.email = reader.GetString(7); Console.WriteLine($"\nFirrst Nmae::{model.first_name}\nlast name::{model.last_name}\naddress::{model.address}\ncity::{model.city}\nstate::{model.state}\nzip::{model.zip}\nphone_number::{model.phone_number}\nemail::{model.email}"); Console.WriteLine("--------------\n"); } } } } } } catch (Exception e) { throw new Exception(e.Message); } finally { connection.Close(); } }