首页 > 解决方案 > textBox3 特定密钥以创建帐户

问题描述

我正在和朋友一起编写应用程序,但在弄清楚如何使第三个文本框包含某个单词/键以继续创建帐户时遇到了一些麻烦

这是表格的样子

在此处输入图像描述

然后这是创建帐户的数据库部分的完整代码。

private void button5_Click(object sender, EventArgs e)
        {
            if (!textBox1.Text.Equals("") && !textBox2.Text.Equals("") && textBox2.Text.Equals(textBox3.Text))
            {
                StringBuilder sb = new StringBuilder();
                using (SHA256 hash = SHA256Managed.Create())
                {
                    Encoding enc = Encoding.UTF8;
                    Byte[] result = hash.ComputeHash(enc.GetBytes(textBox2.Text));

                    foreach (Byte b in result)
                    {
                        sb.Append(b.ToString("x2"));
                    }
                }

                string connectionString = "datasource=127.0.0.1;port=3306;username=root;password=;database=majorpayne;";

                string query = "INSERT INTO staff(USERNAME, PASSWORD) VALUES('" + textBox1.Text + "', '" + sb.ToString() + "')";
                string query2 = "SELECT * FROM staff WHERE username='" + textBox1.Text + "' AND password='" + sb.ToString() + "'";

                MySqlConnection con = new MySqlConnection(connectionString);
                MySqlConnection databaseConnection = new MySqlConnection(connectionString);
                MySqlCommand insertCommand = new MySqlCommand(query, databaseConnection);
                MySqlCommand checkCommand = new MySqlCommand(query2, databaseConnection);
                MySqlDataReader reader;

                try
                {
                    databaseConnection.Open();
                    reader = insertCommand.ExecuteReader();
                    reader.Close();
                    reader = checkCommand.ExecuteReader();

                    if (reader.HasRows)
                    {
                        MessageBox.Show("Successfully Created Account.");
                        {
                            Login main = new Login();
                            main.Show();
                            this.Hide();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Database Error (404)");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
    }
}

如果有人可以帮助使 textBox3 等于特定单词,请提前致谢。

作为一个简短的解释,我希望 textBox3 中有一个“密钥”,用于检查密钥是否是确切的密钥,如果正确的密钥在那里,它会继续并使用输入的用户/密码创建帐户.

标签: c#

解决方案


你可以在 textBox3 下添加标签 然后在 textBox3_KeyUp 事件上写你的代码这样

private void textBox3_KeyUp(object sender, KeyEventArgs e)
    {
        if (textBox1.Text == "Your Key")
        {
            label1.Text = "correct key";
            label1.ForeColor = System.Drawing.Color.Green;
        }
        else
        {
            label1.Text = "wrong key";
            label1.ForeColor = System.Drawing.Color.Red;
        }
    }

推荐阅读