1. 程式人生 > >Photon Server伺服器端(一)

Photon Server伺服器端(一)

目錄

1.控制檯應用程式跟MySQL建立連線

       新增引用MySql.Data.dll

            記得引入名稱空間:using MySql.Data.MySqlClient;


static void Main(string[] args)
        {
            string connectStr = "server=127.0.0.1;port=3306;database=my_schema;user=root;password=root";
            MySqlConnection conn = new MySqlConnection(connectStr);
            try
            {
                conn.Open();
                Console.WriteLine("已經建立連線");
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }finally
            {
                conn.Close();
            }
            Console.ReadKey();
        }

2.利用建立好的連結執行查詢命令MySQLDataReader

static void Main(string[] args)
        {
            string connectStr = "server=127.0.0.1;port=3306;database=my_schema;user=root;password=root";
            MySqlConnection conn = new MySqlConnection(connectStr);
            try
            {
                conn.Open();
                string sql = "select * from users";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                //cmd.ExecuteReader();執行一些查詢
                //cmd.ExecuteNonQuery();插入  刪除
                //cmd.ExecuteScalar();執行一些查詢,返回一個單個的值
                MySqlDataReader reader = cmd.ExecuteReader();
                reader.Read();//讀取下一頁資料,如果讀取成功,返回true;如果沒有下一頁了,讀取失敗的話,返回false;
                Console.WriteLine(reader[0].ToString() + reader[1].ToString() + reader[2].ToString());
                reader.Read();
                Console.WriteLine(reader[0].ToString() + reader[1].ToString() + reader[2].ToString());
                Console.WriteLine("---------------------------------");
                while (reader .Read())
                {
                    reader.Read();
                    //Console.WriteLine(reader[0].ToString() + reader[1].ToString() + reader[2].ToString());
                    Console.WriteLine(reader .GetInt32 ("id")+reader .GetString ("username")+reader .GetString ("password"));//相比上面這種方式更加直觀
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }finally
            {
                conn.Close();
            }
            Console.ReadKey();
        }

 

3.利用程式對資料進行插入Insert操作

string connectStr = "server=127.0.0.1;port=3306;database=my_schema;user=root;password=root";
            MySqlConnection conn = new MySqlConnection(connectStr);
            try
            {
                conn.Open();
                string sql = "insert into users(username,password) values ('lalala','12345')";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                int result = cmd.ExecuteNonQuery();//返回值是資料庫中受影響的資料的行數
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();

 =》  

4.利用程式對資料進行更新Update和刪除Delete操作

  • Update:
            string connectStr = "server=127.0.0.1;port=3306;database=my_schema;user=root;password=root";
            MySqlConnection conn = new MySqlConnection(connectStr);
            try
            {
                conn.Open();
                string sql = "update users set username='aaaaa', password='1111' where id=2 ";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                int result = cmd.ExecuteNonQuery();//返回值是資料庫中受影響的資料的行數
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();

=》

  • delete:
string connectStr = "server=127.0.0.1;port=3306;database=my_schema;user=root;password=root";
            MySqlConnection conn = new MySqlConnection(connectStr);
            try
            {
                conn.Open();
                string sql = "delete from users where id=4 ";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                int result = cmd.ExecuteNonQuery();//返回值是資料庫中受影響的資料的行數
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();

=》

5.利用ExecuteScalar查詢得到一個值的結果

string connectStr = "server=127.0.0.1;port=3306;database=my_schema;user=root;password=root";
            MySqlConnection conn = new MySqlConnection(connectStr);
            try
            {
                conn.Open();
                string sql = "select count(*) from users ";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                //MySqlDataReader reader = cmd.ExecuteReader();
                //reader.Read();
                //int count= Convert .ToInt32 (reader[0].ToString());這種方式麻煩
                object o = cmd.ExecuteScalar();//執行一些查詢,返回一個單個的值(這種方式相對簡單)
                int count = Convert.ToInt32(o.ToString());
                Console.WriteLine(count);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();

6.在查詢的時候新增引數 

        static void Main(string[] args)
        {
            Console.WriteLine(VerifyUser("aaaaa", "1111"));
            Console.WriteLine(VerifyUser("aaaaa", "1"));
            Console.ReadKey();
        }
        static bool VerifyUser(string username, string password)
        {
            string connectStr = "server=127.0.0.1;port=3306;database=my_schema;user=root;password=root";
            MySqlConnection conn = new MySqlConnection(connectStr);
            try
            {
                conn.Open();
                //string sql = "select * from users where username='" + username + "' and password='" + password + "'";
                //這種方式一看就不直觀,因為分號太多,容易搞混。所有還是下面這種方式比較好
                //MySqlCommand cmd = new MySqlCommand(sql, conn);

                string sql = "select * from users where [email protected] and [email protected]";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("username", username);
                cmd.Parameters.AddWithValue("password", password);

                MySqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    return true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                conn.Close();
            }
            return false;
        }