public void SaveEmployee(Employee employee) { try { using (SqlConnection connection = new SqlConnection(_connectionString)) { using (var cmd = connection.CreateCommand()) { cmd.CommandText = "UPDATE Employees SET FirstName = @firstName, LastName = @lastName, Country = @country, Title = @title where EmployeeID = @employeeId "; cmd.Parameters.AddWithValue("@employeeId", employee.EmployeeId); cmd.Parameters.AddWithValue("@firstName", employee.FirstName); cmd.Parameters.AddWithValue("@lastName", employee.LastName); cmd.Parameters.AddWithValue("@country", employee.Country); cmd.Parameters.AddWithValue("@title", employee.Title); connection.Open(); cmd.ExecuteNonQuery(); } } } catch (FaultException<SqlException> ex) { throw new FaultException<SqlException>(ex.Detail); } catch (FaultException ex) { throw new FaultException(ex.Message); } }
public void UpdateEmployee(Employee employee) { try { using (var context = new NORTHWNDEntities()) { var employeeToUpdate = context.Employees.Where(x => x.EmployeeID == employee.EmployeeID).Select(x => x).FirstOrDefault(); if (employeeToUpdate != null) { employeeToUpdate.Address = employee.Address; employeeToUpdate.BirthDate = employee.BirthDate; employeeToUpdate.City = employee.City; employeeToUpdate.Country = employee.Country; employeeToUpdate.Extension = employee.Extension; employeeToUpdate.FirstName = employee.FirstName; employeeToUpdate.HireDate = employee.HireDate; employeeToUpdate.HomePhone = employee.HomePhone; employeeToUpdate.LastName = employee.LastName; employeeToUpdate.Notes = employee.Notes; employeeToUpdate.PostalCode = employee.PostalCode; employeeToUpdate.Region = employee.PostalCode; employeeToUpdate.ReportsTo = employee.ReportsTo; employeeToUpdate.Title = employee.Title; employeeToUpdate.TitleOfCourtesy = employee.TitleOfCourtesy; } context.SaveChanges(); } } catch (Exception ex) { throw new FaultException(ex.Message); } }
public Employee GetEmployees(int id) { try { var queryString = "SELECT * FROM Employees WHERE EmployeeID =" + id; var employee = new Employee(); using (SqlConnection connection = new SqlConnection(_connectionString)) { SqlCommand command = new SqlCommand(queryString, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { employee.EmployeeId = int.Parse(reader["EmployeeID"].ToString()); employee.FirstName = reader["FirstName"].ToString(); employee.LastName = reader["LastName"].ToString(); employee.Country = reader["Country"].ToString(); employee.Title = reader["Title"].ToString(); } } return employee; } catch (FaultException<SqlException> ex) { throw new FaultException<SqlException>(ex.Detail); } catch (FaultException ex) { throw new FaultException(ex.Message); } }