public Worker GetWorkerById(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.fName = reader["fName"].ToString();
                   res.lName = reader["lName"].ToString();
                   res.Position = reader["Position"].ToString();
                   res.Phone = reader["Phone"].ToString();

                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return res;
        }
        public void AddWorker(Worker w)
        {
            sql = "insert into Workers values ('" + w.fName + "', '" + w.lName + "', '" + w.Position + "', '" + w.Phone + "');";

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

                    cmd.ExecuteNonQuery();
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
예제 #3
0
        static void Main(string[] args)
        {
            Worker wrk = new Worker();
               // wrk.Id = 5;
            wrk.fName = "Alex";
            wrk.lName = "CT";
            wrk.Position = "CT";
            wrk.Phone = "123";

            IDepository repo = new DBDepository();

            Console.WriteLine("Added");
            //repo.AddWorker(wrk);
            Console.WriteLine("Removed");
            //repo.DeleteWorker(24);
            Console.WriteLine("Updated");
            repo.UpdateWorker(24, wrk);
            var workers = repo.GetWorkers();
            foreach (var w in workers)
            {
                Console.WriteLine(
                        w.Id + " " +
                        w.fName + " " +
                        w.lName + " " +
                        w.Position + " " +
                        w.Phone + " "
                    );
            }
            Console.WriteLine();
            var workerById = repo.GetWorkerById(2);
            Console.WriteLine(
                        workerById.Id + " " +
                        workerById.fName + " " +
                        workerById.lName + " " +
                        workerById.Position + " " +
                        workerById.Phone + " "
                    );
        }
        public List<Worker> GetWorkers()
        {
            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.fName = reader["fName"].ToString();
                        tmp.lName = reader["lName"].ToString();
                        tmp.Position = reader["Position"].ToString();
                        tmp.Phone = reader["Phone"].ToString();

                        res.Add(tmp);
                    }
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return res;
        }
        public void UpdateWorker(int id, Worker w)
        {
            sql = "update Workers set fName = '" + w.fName + "', lName = '" + w.lName + "', Position = '" + w.Position + "', Phone = '" + w.Phone + "' where id =" + id;

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

                    cmd.ExecuteNonQuery();
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
 public void UpdateWorker(int id, Worker w)
 {
     throw new NotImplementedException();
 }
 public void AddWorker(Worker w)
 {
     throw new NotImplementedException();
 }