Exemplo n.º 1
0
        /// <summary>
        ///     Оновлює інформацію про користувача
        /// </summary>
        public override void Update()
        {
            Console.WriteLine("Виберіть номер користувача та жміть Enter: ");
            Thread.Sleep(2000);
            this.Select();
            Console.WriteLine("Ведіть Id: ");
            int Id = 0;

            Id = EnterIntValue();

            string query = "UPDATE [dbo].[tblUser] SET ";

            bool isBegin = true;

            Console.Write("Ведіть ім'я: ");
            string name = Console.ReadLine();

            if (!string.IsNullOrEmpty(name))
            {
                isBegin = false;
                query  += "Name = N'" + name + "'";
            }

            Console.Write("Ведіть номер телефону: ");
            string tel = Console.ReadLine();

            if (!string.IsNullOrEmpty(tel))
            {
                if (!isBegin)
                {
                    query += ", ";
                }
                else
                {
                    isBegin = false;
                }
                query += "telNummer = " + "N'" + tel + "'";
            }

            City   city   = null;
            string cityId = "1";

            while (city == null)
            {
                Console.Write("Ведіть номер міста: ");
                cityId = Console.ReadLine();
                if (!string.IsNullOrEmpty(cityId))
                {
                    if (UserCRUD.cities == null)
                    {
                        UserCRUD.cities = UserCRUD.GetCitiesFromDB(this.sql);
                    }
                    try
                    {
                        city = UserCRUD.cities.Where(delegate(City c) { return(c.id == int.Parse(cityId)); }).FirstOrDefault();
                    }
                    catch
                    {
                        Console.Clear();
                        Console.WriteLine("Помилка вводу данних (процес анульовано)");
                        Thread.Sleep(1000);
                        return;
                    }
                }
                else
                {
                    break;
                }
            }

            if (!string.IsNullOrEmpty(cityId))
            {
                if (!isBegin)
                {
                    query += ", ";
                }
                else
                {
                    isBegin = false;
                }
                try
                {
                    query += "CityId = " + "N'" + int.Parse(cityId) + "'";
                }
                catch
                {
                    Console.Clear();
                    Console.WriteLine("Помилка вводу данних (процес анульовано)");
                    Thread.Sleep(1000);

                    return;
                }
            }

            query += " WHERE Id = " + Id;
            if (!isBegin)
            {
                SqlCommand command = new SqlCommand(query, this.sql);
                command.ExecuteNonQuery();
                this.users = null;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Створює нових користувачів і додає у БД
        /// </summary>
        public override void Create()
        {
            Console.Clear();
            Console.Write("Ведіть ім'я: ");
            string name = Console.ReadLine();

            Console.Write("Ведіть номер телефону: ");
            string telNummer = Console.ReadLine();



            int skipped = 0;

            if (UserCRUD.cities == null)
            {
                UserCRUD.cities = UserCRUD.GetCitiesFromDB(this.sql);
            }

            ConsoleKeyInfo keyInfo = new ConsoleKeyInfo();

            while (keyInfo.Key != ConsoleKey.Enter)
            {
                Console.Clear();
                Console.WriteLine("Перегляньте міста та виберіть Id і жміть Enter:");
                foreach (var item in UserCRUD.cities.Skip(skipped).Take(5))
                {
                    Console.WriteLine(item);
                }
                Console.SetCursorPosition(15, 6);
                Console.WriteLine("<<<< Enter >>>>");
                keyInfo = Console.ReadKey();

                switch (keyInfo.Key)
                {
                case ConsoleKey.LeftArrow:
                {
                    if (skipped > 0)
                    {
                        skipped -= 5;
                    }
                    break;
                }

                case ConsoleKey.RightArrow:
                {
                    if (skipped + 5 < UserCRUD.cities.Count)
                    {
                        skipped += 5;
                    }
                    break;
                }
                }
            }

            City c      = null;
            int  cityId = 1;

            while (c == null)
            {
                Console.Clear();
                Console.WriteLine("Ведіть Id: ");

                cityId = EnterIntValue();


                c = UserCRUD.cities.Where(c => c.id == cityId).FirstOrDefault();
            }


            string     query   = $"INSERT INTO [dbo].[tblUser] (Name, telNummer, CityId) VALUES (N'{name}', N'{telNummer}', N'{cityId}')";
            SqlCommand command = new SqlCommand(query, this.sql);

            command.ExecuteNonQuery();

            this.users = null;
        }