예제 #1
0
        /// <summary>
        /// Add random value in table Journal
        /// </summary>
        /// <param name="db">DbContext</param>
        /// <param name="count">Count</param>
        public static void AddJournal(Academy db, int count, int minRating, int maxRating)
        {
            var sList = db.Students.ToList();
            var dList = db.Disciplines.ToList();

            for (int i = 0; i < count; i++)
            {
                db.Journal.Add(new Journal()
                {
                    StudentId    = sList[rand.Next(sList.Count)].Id,
                    DisciplineId = dList[rand.Next(dList.Count)].Id,
                    Rating       = rand.Next(minRating, maxRating),
                    Moment       = new DateTime(2020, 01, rand.Next(1, 32), rand.Next(8, 16), rand.Next(60), rand.Next(60))
                });
            }
        }
예제 #2
0
        /// <summary>
        /// Add random names in table Students
        /// </summary>
        /// <param name="db">DbContext</param>
        /// <param name="count">Count</param>
        public static void AddStudents(Academy db, int count)
        {
            SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=RandomDB;Integrated Security=True;");

            connection.Open();
            SqlCommand cmd = new SqlCommand("select Name from Names order by newid()", connection);

            for (int i = 0; i < count; i++)
            {
                db.Students.Add(new Student()
                {
                    Name = (string)cmd.ExecuteScalar()
                });
            }

            connection.Close();
        }
예제 #3
0
        static void Main()
        {
            //-----------------------------------------------------------------
            Academy db = new Academy("DefaultConnection");
            //-----------------------------------------------------------------

            //-----------------------------------------------------------------
            //Insert.AddStudents(db, 50);
            //Insert.AddDisciplines(db);
            //Insert.AddJournal(db, 10000, 5, 12);
            //db.SaveChanges();
            //-----------------------------------------------------------------

            //-------------------------------------------
            int top = EnterInt("Top");
            //-------------------------------------------
            var q = from j in db.Journal
                    join s in db.Students on j.StudentId equals s.Id
                    join d in db.Disciplines on j.DisciplineId equals d.Id
                    select new { j, s, d };

            PrintTake();
            //-------------------------------------------
            int jId = EnterInt("JId");

            Journal jItem = db.Journal.Find(jId);

            if (jItem == null)
            {
                Console.WriteLine("Not found");
            }
            else
            {
                db.Journal.Remove(jItem);
                db.SaveChanges();

                PrintTake();
            }
            //-------------------------------------------
            int sId = EnterInt("SId");

            Student sItem = db.Students.Find(sId);

            if (sItem == null)
            {
                Console.WriteLine("Not found");
            }
            else
            {
                Console.Write("New name: ");
                sItem.Name = Console.ReadLine();
                db.SaveChanges();

                PrintWhere();
            }
            //-------------------------------------------

            //-----------------------------------------------------------------
            Console.WriteLine(new string('-', 64));
            Console.WriteLine($"Students: {db.Students.Count()}");
            Console.WriteLine($"Disciplines: {db.Disciplines.Count()}");
            Console.WriteLine($"Journal: {db.Journal.Count()}");
            //-----------------------------------------------------------------

            //-----------------------------------------------------------------
            db.Dispose();
            Console.ReadKey();
            //-----------------------------------------------------------------

            //-------------------------------------------
            void Print(dynamic a)
            {
                Console.WriteLine("JId [" + $"{a.j.Id}".PadLeft(5, '0') + "]".PadRight(8)
                                  + "SId [" + $"{a.s.Id}".PadLeft(3, '0') + "]".PadRight(8)
                                  + $"{a.s.Name}".PadRight(12)
                                  + $"{a.d.Title}".PadRight(24)
                                  + $"{a.j.Rating}".PadLeft(2, '0'));
            }

            //-------------------------------------------
            void PrintTake()
            {
                Console.WriteLine();
                foreach (var a in q.Take(top))
                {
                    Print(a);
                }
            }

            //-------------------------------------------
            void PrintWhere()
            {
                Console.WriteLine();
                foreach (var a in q.Where(a => a.s.Id == sId))
                {
                    Print(a);
                }
            }

            //-------------------------------------------
        }