コード例 #1
0
        public Worker Get(int id)
        {
            sql = "select Id , fName , lName , Position , Phone from Workers where id = " + id;
            Worker res = new Worker();
            try
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    SqlDataReader reader = cmd.ExecuteReader();
                    reader.Read();

                    res.Id = Convert.ToInt32(reader["Id"]);
                    res.FirstName = reader["fName"].ToString();
                    res.LastName = reader["lName"].ToString();
                    res.Position = reader["Position"].ToString();
                    res.Phone = reader["Phone"].ToString();

                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return res;
        }
コード例 #2
0
        public Worker Add(Worker item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("Item Is Null");

            }
            item.Id = _nextId++;
            _workers.Add(item);

            return item;
        }
コード例 #3
0
        public Worker Add(Worker w)
        {
            sql = "insert into Workers values ('" + w.FirstName + "', '" + w.LastName + "', '" + w.Position + "', '" + w.Phone + "');";

            try
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    cmd.ExecuteNonQuery();
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            return w;
        }
コード例 #4
0
        public bool Update(Worker item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("Item Is Null");

            }
            int i = _workers.FindIndex(w => w.Id == item.Id);

            if (i == -1)
            {
                return false;
            }
            _workers.RemoveAt(i);
            _workers.Add(item);
            _workers = _workers.OrderBy(w => w.Id).ToList();

            return true;
        }
コード例 #5
0
        public IEnumerable<Worker> GetAll()
        {
            List<Worker> res = new List<Worker>();
            sql = "select Id ,  fName , lName , Position , Phone from Workers";
            try
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        Worker tmp = new Worker();
                        tmp.Id = Convert.ToInt32(reader["Id"]);
                        tmp.FirstName = reader["fName"].ToString();
                        tmp.LastName = reader["lName"].ToString();
                        tmp.Position = reader["Position"].ToString();
                        tmp.Phone = reader["Phone"].ToString();

                        res.Add(tmp);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return res;
        }
コード例 #6
0
        public bool Update(Worker w)
        {
            //int ID = _workers.FindIndex(wr => w.Id == w.Id);
            sql = "update Workers set fName = '" + w.FirstName + "', lName = '" + w.LastName + "', Position = '" + w.Position + "', Phone = '" + w.Phone + "' where id =" + w.Id;

            try
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    cmd.ExecuteNonQuery();
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            return true;
        }