示例#1
0
文件: Form1.cs 项目: BJLU/EvCode
        private static void AddUser() // method for defined add new User to the database
        {
            Form1 form = new Form1();

            form.SendingData("introduce User's Name\n"); // call method SendingData for send current string
            var    userName = form.ReceivingData();      // call method Receiving for got data from client part
            string userN    = userName.ToString();       // convert get data to the type string

            form.SendingData("introduce User's Age");    // call method SendingData for send current string
            var userAge = form.ReceivingData();          // call method ReceivingData for get all data from client part
            int userA   = Int32.Parse(userAge);          // convert get data in the int type

            using (UserContext db = new UserContext())   // defined variable about class UserContext
            {
                User newUser = new User {
                    Name = userN, Age = userA
                };                                     // defined instance with got data

                db.Users.Add(newUser);                 // choice current object and add to the database
                db.SaveChanges();                      // save new data in the database
                form.SendingData("command performed"); // call method SendingData for send current string

                var users = db.Users;                  // got all users from the database

                form.UsersEvent += form.ShowUsers;     // defined current method 'ShowUsers' for event - 'UsersEvent'
                if (form.UsersEvent != null)           // if event have method for performing
                {
                    form.UsersEvent(users);            // call current event
                }
            }
        }
示例#2
0
文件: Form1.cs 项目: BJLU/EvCode
        private static void DeleteUser()               // method for deleting certain user in the database
        {
            Form1 ser2 = new Form1();                  // create instance class ServerKernel

            ser2.SendingData("introduce User's Id");   // call method SendingData for send current string
            var userId = ser2.ReceivingData();         // call method ReceivingData for get all data from client part
            int userI  = Int32.Parse(userId);          // convert get clients data to the int type

            using (UserContext db = new UserContext()) // defined instance about class UserContext
            {
                var users = db.Users;                  // got all users from database
                foreach (User u in users)              // get access about each user in the database
                {
                    if (u.Id == userI)                 // if id will searched in the database then
                    {
                        db.Users.Remove(u);            // delete current user from database
                        break;                         // and will stop current increment
                    }
                }
                db.SaveChanges();                                   // save database after delete certain user
                ser2.SendingData("command performed");              // call method SendingData for send current string
                var usersAll = db.Users;                            // got data about all users from database
                ser2.SendingData("all Users from database");        // call method SendingData for send current string
                foreach (User u in usersAll)                        // will got access to the every user in the database
                {
                    ser2.SendingData($"{u.Id}.{u.Name} - {u.Age}"); // will call method SendingData for send certain data about current user
                }
            }
        }