MySQL in C # code for interactive exercises

the using the System;
 the using the System.Collections.Generic;
 the using the System.Linq;
 the using the System.Text;
 the using System.Threading.Tasks;
 the using the MySql.Data.MySqlClient; 

namespace CSharpToPhotonServer 
{ 
    class Program 
    { 
        // cmd.ExecuteReader ();         // out some queries
         // the cmd.ExecuteNonQuery ();       // insertion, deletion
         // cmd.ExecuteScalar ();         // perform some query returns a single value 
        Private  static  void the Main ( String  [] args)
        {
            string connectIP = @"server = XXXXX.com;";
            string connectPort = @"port = ?????;";
            string connectUser = @"user = root;";
            string connectPassword = @"password = XXXXX;";
            string connectDatabase = @"database = MyGameDB;";

            MySqlConnection conn = new MySqlConnection(connectIP + connectPort + connectUser + connectPassword + connectDatabase);
            try
            {
                conn.Open();
                Console.WriteLine("已经建立连接");

                string sqlInsert = "INSERT INTO `MyGameDB`.`Account` (`AccountNum`, `AccountPassword`, `AccountName`, `registerDate`)";
                string sqlInsertVALUES = "VALUES ('1264881', 'sdhgdd6', 'fsddSHhjdj', '" + DateTime.Now +"');";
                //MySqlInsert(sqlInsert + sqlInsertVALUES, conn);       SQLReader =Stringinsert//

                "SELECT * FROM MyGameDB.Account;";
                //MySqlReader(sqlReader, conn);     //查询

                string sqlUpdate = "UPDATE `MyGameDB`.`Account` SET `AccountVIP` = 'YES', `AccountLevel` = '9' WHERE (`ID` = '2');";
                //MySqlUpdate(sqlUpdate, conn);     //修改

                int DeleteID = 11;
                string sqlDelete = "DELETE FROM `MyGameDB`.`Account` WHERE (`ID` = '" + DeleteID + "');";
                //MySqlDelete(sqlDelete, conn);       //删除


                string sqlCount = "select count(*) from MyGameDB.Account;";
                //MySqlDataReader reader = MySqlCount(sqlCount, conn);
                //reader.Read();
                //Console.WriteLine(reader[0]);

                //object o = MySqlCount_2(sqlCount, conn);
                //int outNum = Convert.ToInt32(o);
                //Console.WriteLine(outNum);


                Console.WriteLine(VerifyUser("XXXXX", "XXXXX", conn));

            }
            catch (Exception e)
            {
                Console.WriteLine("连接失败");
                Console.WriteLine(e.ToString());
            }
            finally
            {
                conn.Close();
                Console.WriteLine("连接已关闭");
            }

            Console.ReadKey();


            MySqlDataReader MySqlCount(string sql, MySqlConnection coon)
            {
                try
                {
                    MySqlCommand cmd = newThe MySqlCommand (SQL, Coon); 
                    the MySqlDataReader Reader = cmd.ExecuteReader ();      // return value is affected by the database rows of data 
                    Console.WriteLine ( " query data successfully " );
                     return Reader; 
                } 
                the catch 
                { 
                    Console.WriteLine ( " query data failed " );
                     return  null ; 
                } 
            } 

            Object MySqlCount_2 ( String SQL, the MySqlConnection Coon) 
            { 
                the try 
                {
                    Cmd the MySqlCommand = new new the MySqlCommand (SQL, Coon);
                     Object O = cmd.ExecuteScalar ();      // return value is affected by the database rows of data 
                    Console.WriteLine ( " query data successfully " );
                     return O; 
                } 
                the catch 
                { 
                    Console.WriteLine ( " query data failed " );
                     return  null ; 
                } 
            } 

            void MySqlDelete ( String  SQL, the MySqlConnection Coon)
            { 
                the try 
                { 
                    the MySqlCommand cmd = new new the MySqlCommand (SQL, Coon);
                     int Result = the cmd.ExecuteNonQuery ();      // return value is affected by the database data rows 
                    Console.WriteLine ( " successfully removed [ " + Result + " ] pieces of data " ); 
                } 
                the catch 
                { 
                    Console.WriteLine ( " delete data failed " ); 
                } 
            } 


            void MySqlUpdate ( StringSQL, the MySqlConnection Coon) 
            {
                the try 
                { 
                    the MySqlCommand cmd = new new the MySqlCommand (SQL, Coon);
                     int Result = the cmd.ExecuteNonQuery ();      // return value is affected by the database rows of data 
                    Console.WriteLine ( " successfully updated [ " + Result + " ] Article data " ); 
                } 
                the catch 
                { 
                    Console.WriteLine ( " update data failed " ); 
                } 
            } 

            void MySqlInsert ( SQL, the MySqlConnection Coon)String 
            { 
                the try 
                { 
                    the MySqlCommand cmd = new new the MySqlCommand (SQL, Coon);
                     int Result = the cmd.ExecuteNonQuery ();      // return value is affected by the database rows of data 
                    Console.WriteLine ( " Successful insertion [ " + Result + " ] pieces of data " ); 
                } 
                the catch 
                { 
                    Console.WriteLine ( " insert data failed " ); 
                } 
                

            } 

            void MySqlReader ( String sql, MySqlConnection coon)
            {
                MySqlCommand cmd = new MySqlCommand(sql, coon);
                MySqlDataReader reader = cmd.ExecuteReader();

                //reader.Read();
                //ReaderDB(reader);
                
                while(reader.Read() == true)
                {
                    ReaderDB(reader);
                }



            }

            void ReaderDB(MySqlDataReader reader)
            {
                try
                {
                    Console.Write("ID: " + reader.GetInt32(0));
                    Console.Write("\t" + "Num: " + reader.GetInt32(1) + "\t");
                    Console.Write("\t" + "Password: " + reader.GetString(2));
                    Console.Write("\t" + "Name: " + reader.GetString(3) + "\t");
                    Console.Write("\t" + "VIP: " + reader.GetString(4) + "\t");
                    Console.WriteLine("\t" + "Level: " + reader.GetInt32(5));
                }
                catch
                {
                    Console.WriteLine("读取失败");
                }
            }



        }

        private static bool VerifyUser(string userName, string userPassword, MySqlConnection coon)
        {
            try
            {
                //string sql = "SELECT * FROM MyGameDB.Account where AccountNum = '" + userName + "' and AccountPassword = '" + userPassword + "';";
                string sql = "SELECT * FROM MyGameDB.Account where AccountNum = @userNum and AccountPassword = @Password;";

                MySqlCommand cmd = new MySqlCommand(sql, coon);
                cmd.Parameters.AddWithValue("userNum", userName);
                cmd.Parameters.AddWithValue("Password" , The userPassword); 

                the MySqlDataReader Reader = cmd.ExecuteReader ();
                 IF (reader.Read () == to true ) 
                { 
                    Console.WriteLine ( " the verification is successful, registration is completed! " );
                     Return  to true ; 
                } 
                the else 
                { 
                    Console.WriteLine ( " unknown error " ); 
                } 
            } 
            the catch 
            { 
                Console.WriteLine ( " user name and password errors, failed authentication "); 
            } 
            Return  to false ; 
        } 

        // Private static void MySqlFun (String SQL, the MySqlConnection Coon)
         // {
         //     the MySqlCommand the MySqlCommand new new cmd = (SQL, Coon);
         //     // cmd.ExecuteReader ();         // do some query
         //     // the cmd.ExecuteNonQuery ();       // insertion, deletion
         //     // cmd.ExecuteScalar ();         // perform some query returns a single value 

        //     the MySqlDataReader = cmd.ExecuteReader Reader (); 

        //     Reader .read ();
         //    Console.WriteLine(reader[0].ToString() + reader[1].ToString() + reader[2].ToString() + reader[3].ToString() + reader[4].ToString() + reader[5].ToString());
        
        //}

    }
}

 

Guess you like

Origin www.cnblogs.com/91-JiaoTeacher/p/11600235.html