登入介面:從資料庫中獲取資訊驗證登入(與註冊介面相聯絡)

yunduanzhijie發表於2019-08-05

 /// <summary>
    ///與登入介面有關的集合在這裡,ExecuteQuery方法在https://blog.csdn.net/yunduanzhijie/article/details/98488594
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Button1_Click_1(object sender, EventArgs e)
        {
            DataTable d1 = new DataTable();
            String name = this.textBox2.Text; // 獲取裡面的值
            String password = this.textBox1.Text;
            Form2 f2 = new Form2();
            //查詢並判斷使用者名稱是否存在
            string sql_select = "SELECT password FROM [study-userset] where username='" + name + "'";
            d1=f2.ExecuteQuery(sql_select);
            if (d1.Rows.Count>0)
            {
                string pw = d1.Rows[0]["password"].ToString();
                if (f2.ComputeMD5Hash(password).Equals(pw)) // 判斷賬號密碼是否相同
                {
                    MessageBox.Show("登入成功!");
                }
                else
                {
                    MessageBox.Show("密碼錯誤!");
               
                }
            }
            else
            {
                MessageBox.Show("未取得使用者名稱!");
            }
        }

        private void TextBox1_Enter(object sender, EventArgs e)
        {
            //改變活動時的樣式
            textBox1.ForeColor = Color.Black;
            textBox1.BackColor = Color.Gainsboro;
        }

        private void TextBox1_Leave(object sender, EventArgs e)
        {
            textBox1.ForeColor = Color.Gainsboro;
            textBox1.BackColor = Color.White;
        }

        private void TextBox2_Enter(object sender, EventArgs e)
        {
            textBox2.ForeColor = Color.Black;
            textBox2.BackColor = Color.Gainsboro;
        }

        private void TextBox2_Leave(object sender, EventArgs e)
        {
            textBox2.ForeColor = Color.Gainsboro;
            textBox2.BackColor = Color.White;
        }

        private void Button1_Click_2(object sender, EventArgs e)
        {
            //關閉、開啟頁面
            Form1 f = new Form1();
            Form2 f2 = new Form2();
            f2.Show();
            f.Close();
        }
    }

相關文章