Пример #1
0
        public async Task<IHttpActionResult> PutEmployee(int id, Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != employee.EmployeeID)
            {
                return BadRequest();
            }

            db.Entry(employee).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Пример #2
0
        public Employee GetByID(int iD)
        {
            using (
                SqlConnection cn =
                    new SqlConnection(ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString))
            {
                var cmd =
                    new SqlCommand(
                        "SELECT FirstName, LastName, BirthDate, EmployeeID FROM Employees WHERE EmployeeID = @ID", cn);
                cmd.Parameters.AddWithValue("@ID", iD);

                cn.Open();
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        var temp = new Employee();
                        temp.FirstName = dr.GetString(0);
                        temp.LastName = dr.GetString(1);
                        temp.DateOfBirth = dr["BirthDate"] is DBNull ? DateTime.Now : dr.GetDateTime(2);
                        temp.EmployeeID = dr.GetInt32(3);
                        return temp;
                    }
                }
            }
            return null;
        }
Пример #3
0
        protected void Page_Load(object sender, EventArgs e)
        {
            this.backUrl = Request.UrlReferrer.AbsolutePath;
            var idAsString = Request.QueryString["id"];
            if (idAsString == null) Response.Redirect("/");

            var id = int.Parse(idAsString);
            this.employee = new NorthwindEntities().Employees
                .FirstOrDefault(empl => empl.EmployeeID == id);
        }
Пример #4
0
        public async Task<IHttpActionResult> PostEmployee(Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Employees.Add(employee);
            await db.SaveChangesAsync();

            return CreatedAtRoute("DefaultApi", new { id = employee.EmployeeID }, employee);
        }
Пример #5
0
 public void Delete(Employee employeeToDelete)
 {
     using (
         SqlConnection cn =
             new SqlConnection(ConfigurationManager.ConnectionStrings["NorthWind"].ConnectionString))
     {
         var cmd = new SqlCommand("DELETE FROM Employees WHERE EmployeeID = @iD", cn);
         cmd.Parameters.AddWithValue("@iD", employeeToDelete.EmployeeID);
         cn.Open();
         cmd.ExecuteNonQuery();
     }
 }
Пример #6
0
        public void Create(Employee employeeToCreate)
        {
            using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString))
            {
                var cmd = new SqlCommand("INSERT INTO Employees (FirstName, LastName) VALUES (@FirstName, @LastName)", cn);
                cmd.Parameters.AddWithValue("@FirstName", employeeToCreate.FirstName);
                cmd.Parameters.AddWithValue("@LastName", employeeToCreate.LastName);

                cn.Open();
                cmd.ExecuteNonQuery();
            }
        }
Пример #7
0
        // POST: odata/Employees
        public async Task<IHttpActionResult> Post(Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Employees.Add(employee);
            await db.SaveChangesAsync();

            return Created(employee);
        }
Пример #8
0
 public List<Employee> GetAll()
 {
     var Employees = new List<Employee>();
     using (
         SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString)
         )
     {
         var cmd = new SqlCommand("SELECT FirstName, LastName,BirthDate FROM Employees", cn);
         cn.Open();
         using (SqlDataReader dr = cmd.ExecuteReader())
         {
             while (dr.Read())
             {
                 var temp = new Employee();
                 temp.FirstName = dr.GetString(0);
                 temp.LastName = dr.GetString(1);
                 temp.DateOfBirth = dr["BirthDate"] is DBNull ? DateTime.Now : dr.GetDateTime(2);
                 Employees.Add(temp);
             }
         }
     }
     return Employees;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="EmployeeUpdatedEvent" /> class
 /// </summary>
 /// <param name="employee">The <see cref="Employee" /> which the event relates to</param>
 public EmployeeUpdatedEvent(Employee employee)
     : base(string.Format("Employee: '{0}' was updated.", employee.FirstName), "Northwind.Web", WebEventCodes.WebExtendedBase + 100)
 {
 }