public DAL.Models.Employee Add(DAL.Models.Employee employee) { DAL.Models.Employee employeeAdded = new Models.Employee(); try { using (sqlConnection) { sqlConnection.Open(); SqlCommand dbCommand = new SqlCommand(SQL.Querys.GetInsertEmployeeQuery(), sqlConnection); AddParameters(dbCommand, employee); using (SqlDataReader sqlDataReader = dbCommand.ExecuteReader()) { while (sqlDataReader.Read()) { employeeAdded = GetEmployee(sqlDataReader); } } sqlConnection.Close(); return(employeeAdded); }; } catch (Exception exception) { throw exception; } }
private void AddParameters(SqlCommand dbCommand, Models.Employee employee) { dbCommand.Parameters.AddWithValue("@CompanyId", employee.CompanyId); dbCommand.Parameters.AddWithValue("@CreatedOn", employee.CreatedOn); dbCommand.Parameters.AddWithValue("@DeletedOn", employee.DeletedOn); dbCommand.Parameters.AddWithValue("@Email", employee.Email); dbCommand.Parameters.AddWithValue("@Fax", employee.Fax); dbCommand.Parameters.AddWithValue("@Name", employee.Name); dbCommand.Parameters.AddWithValue("@Lastlogin", employee.LastLogin); dbCommand.Parameters.AddWithValue("@Password", employee.Password); dbCommand.Parameters.AddWithValue("@PortalId", employee.PortalId); dbCommand.Parameters.AddWithValue("@RoleId", employee.RoleId); dbCommand.Parameters.AddWithValue("@StatusId", employee.StatusId); dbCommand.Parameters.AddWithValue("@Telephone", employee.Telephone); dbCommand.Parameters.AddWithValue("@UpdatedOn", employee.UpdatedOn); dbCommand.Parameters.AddWithValue("@Username", employee.Username); }
public DAL.Models.Employee GetByID(int id) { DAL.Models.Employee employee = new Models.Employee(); bool employeeExists = false; try { using (sqlConnection) { sqlConnection.Open(); SqlCommand dbCommand = new SqlCommand(SQL.Querys.GetSelectByIdQuery(), sqlConnection); dbCommand.Parameters.AddWithValue("@Id", id); using (SqlDataReader sqlDataReader = dbCommand.ExecuteReader()) { while (sqlDataReader.Read()) { employeeExists = true; employee = GetEmployee(sqlDataReader); } } sqlConnection.Close(); if (employeeExists) { return(employee); } else { throw new Exception("the employee does not exist"); } }; } catch (Exception exception) { throw exception; } }