示例#1
0
        public static void Update()
        {
            string query                = "UPDATE users SET";
            string query_firstname      = "";
            string query_lastname       = "";
            string query_favoritenumber = "";
            bool   a = false;
            bool   c = false;
            bool   b = false;

            Console.WriteLine("Want to update database? (yes/no)");
            string confir = Console.ReadLine();

            if (confir == "yes")
            {
                Console.WriteLine("Which user do you want to update ? (type in user ID)");
                string        ID        = Console.ReadLine();
                var           temp1     = DbConnector.Query($"SELECT COUNT(id) FROM users WHERE id = {ID}"); //check if this user exists, by using Count(), return List<Dictionary<string,object>>
                List <object> temp2     = temp1[0].Values.ToList();                                          //get first value which is number of how many matches, USING SYSTEM.LINQ IN ORDER TO USE ToList() to convert KeyCollction to List
                int           existance = Convert.ToInt32(temp2[0]);                                         //Error:"Unable to cast object of type 'System.Int64' to type 'System.Int32'."
                if (existance != 0)
                {
                    Console.WriteLine("want to update first name?(type first name or just type no)");
                    string first_name = Console.ReadLine();
                    if (first_name != "no")
                    {
                        query_firstname = $" first_name = '{first_name}'";
                    }
                    else
                    {
                        a = true;
                    }
                    Console.WriteLine("want to update last name?(type last name or no)");
                    string last_name = Console.ReadLine();
                    if (last_name != "no")
                    {
                        query_lastname = $" ,last_name = '{last_name}'";
                    }
                    else
                    {
                        b = true;
                    }
                    Console.WriteLine("want to update favorite number(type number or no)");
                    string favoritenumber = Console.ReadLine();
                    if (favoritenumber != "no")
                    {
                        query_favoritenumber = $" ,favoritenumber = {favoritenumber}";
                    }
                    else
                    {
                        c = true;
                    }


                    if (a && b && c)
                    {
                        Console.WriteLine(" You did not input, update fails !");
                    }
                    else
                    {
                        query = query + query_firstname + query_lastname + query_favoritenumber + $" WHERE id = {ID}";
                        Console.WriteLine(query);
                        DbConnector.Query(query);
                    }
                }
                else
                {
                    Console.WriteLine("Record does not exist!");
                }
            }
        }