public bool SaveEmployee(Employee employee) { using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["northwindDb"].ToString())) { const string cmdText = "UPDATE [dbo].[Employees] SET [LastName] = @LastName, [FirstName] = @FirstName, [Title] = @Title,[TitleOfCourtesy] = @TitleOfCourtesy,[BirthDate] = @BirthDate,[HireDate] = @HireDate,[Address] = @Address,[City] = @City,[Region] = @Region,[PostalCode] = @PostalCode,[Country] = @Country,[HomePhone] = @HomePhone,[Extension] = @Extension, [Notes] = @Notes WHERE [EmployeeID] = @EmployeeID"; var command = new SqlCommand(cmdText, conn); command.Parameters.AddWithValue("@LastName", employee.LastName); command.Parameters.AddWithValue("@FirstName", employee.FirstName); command.Parameters.AddWithValue("@Title", employee.Title); command.Parameters.AddWithValue("@TitleOfCourtesy", employee.TitleOfCourtesy); command.Parameters.AddWithValue("@BirthDate", employee.BirthDate); command.Parameters.AddWithValue("@HireDate", employee.HireDate); command.Parameters.AddWithValue("@Address", employee.Address); command.Parameters.AddWithValue("@City", employee.City); command.Parameters.AddWithValue("@Region", employee.Region); command.Parameters.AddWithValue("@PostalCode", employee.PostalCode); command.Parameters.AddWithValue("@Country", employee.Country); command.Parameters.AddWithValue("@HomePhone", employee.HomePhone); command.Parameters.AddWithValue("@Extension", employee.Extension); command.Parameters.AddWithValue("@Notes", employee.Notes); command.Parameters.AddWithValue("@EmployeeID", employee.EmployeeID); conn.Open(); return command.ExecuteNonQuery() > 0; } }
public bool SaveEmployee(Employee employee) { try { return repo.SaveEmployee(employee); } catch (Exception ex) { throw new FaultException(ex.Message); } }
public Employee GetEmployee(int employeeId) { using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["northwindDb"].ToString())) { var command = new SqlCommand("SELECT [EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country], [HomePhone], [HomePhone], [Extension], [Notes] FROM [dbo].[Employees] WHERE [EmployeeID] = @EmployeeID", conn); command.Parameters.AddWithValue("@EmployeeID", employeeId); conn.Open(); var employee = new Employee(); using (var reader = command.ExecuteReader()) { while (reader.Read()) { employee.LastName = reader["LastName"].ToString(); employee.FirstName = reader["FirstName"].ToString(); employee.Title = reader["Title"].ToString(); employee.TitleOfCourtesy = reader["TitleOfCourtesy"].ToString(); employee.BirthDate = Convert.ToDateTime(reader["BirthDate"]); employee.HireDate = Convert.ToDateTime(reader["HireDate"].ToString()); employee.Address = reader["Address"].ToString(); employee.City = reader["City"].ToString(); employee.Region = reader["Region"].ToString(); employee.PostalCode = reader["PostalCode"].ToString(); employee.Country = reader["Country"].ToString(); employee.HomePhone = reader["HomePhone"].ToString(); employee.Extension = reader["Extension"].ToString(); employee.Notes = reader["Notes"].ToString(); } } employee.EmployeeID = employeeId; return employee; } }