You are on page 1of 4

using using using using using

System; System.Collections.Generic; System.Linq; System.Text; System.Data.SqlClient;

namespace ConsoleApplication21 { class Program { static void select() { string abc; Console.WriteLine("Enter employee ID"); abc = Console.ReadLine(); SqlConnection connect = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=addressbook;Integrated Security=True"); connect.Open(); SqlCommand command = new SqlCommand(); command.CommandText = "select * from synecontacts"; command.Connection = connect; SqlDataReader reader = null; // command.Parameters.Add("@x", abc); reader = command.ExecuteReader(); while (reader.Read()) { for (int i = 0; i < reader.FieldCount; i++) { Console.Write(reader[i] + " | "); } Console.WriteLine(); } Console.ReadLine(); } static void insert() { string a, c, d; int b; Console.WriteLine("Enter the name"); a = Console.ReadLine(); Console.WriteLine("Enter the employeeid"); b = int.Parse(Console.ReadLine()); Console.WriteLine("Enter then mobile"); c = Console.ReadLine(); Console.WriteLine("Enter then address"); d = Console.ReadLine();

SqlConnection connect = new SqlConnection(); string connecttostring = @"Data Source=.\SQLEXPRESS;Initial Catalog=addressbook;Integrated Security=True"; connect.ConnectionString = connecttostring; connect.Open(); SqlCommand cmd = new SqlCommand(); cmd.Parameters.Add("@x", a); cmd.Parameters.Add("@y", b); cmd.Parameters.Add("@z", c); cmd.Parameters.Add("@w", d); string command = "insert into synecontacts values(@x,@y,@z,@w)"; cmd.CommandText = command; cmd.Connection = connect; SqlDataReader reader = null; reader = cmd.ExecuteReader(); Console.WriteLine("Successfully Inserted"); } static void delete() { string x; Console.WriteLine("Enter the name of the employee whose records needs to be deleted"); x=Console.ReadLine(); SqlConnection connect = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=addressbook;Integrated Security=True"); connect.Open(); SqlCommand command = new SqlCommand("Delete from synecontacts where name=@x"); command.Parameters.Add("@x", x); command.Connection = connect; SqlDataReader reader = null; reader = command.ExecuteReader(); Console.WriteLine("Delete Operation Successful"); } static void update() { string y; int x; Console.WriteLine("Enter the employee id whose corresponding person name needs to be updated"); x = int.Parse(Console.ReadLine()); Console.WriteLine("enter the new name"); y = Console.ReadLine(); SqlConnection connect = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=addressbook;Integrated Security=True"); connect.Open(); SqlCommand command = new SqlCommand("update synecontacts set name=@y where empid=@x"); command.Parameters.Add("@x", x); command.Parameters.Add("@y", y); command.Connection = connect;

SqlDataReader reader = null; try { reader = command.ExecuteReader(); } catch (Exception e) { Console.WriteLine("Exception:{0}",e); } Console.WriteLine("Update Operation Successful"); } static void Main(string[] args) { int i; do { Console.WriteLine("Which Operation You Want to Perform"); Console.WriteLine("Press1 for Select 2 for Update 3 for Insert and 4 for Delete"); i = int.Parse(Console.ReadLine()); switch (i) { case 1: select(); break; case 2: update(); break; case 3: insert(); break; case 4: delete(); break; default : Console.WriteLine("Invalid Input"); break; } Console.WriteLine("Do you want to continue press 1 "); try { i = int.Parse(Console.ReadLine()); } catch (Exception ex)

{ Console.WriteLine("Exception Occured : {0}",ex); } } while (i == 1); } } }