c# - 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 中有一个“密钥”,用于检查密钥是否是确切的密钥,如果正确的密钥在那里,它会继续并使用输入的用户/密码创建帐户.
解决方案
你可以在 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;
}
}
推荐阅读
- javascript - 使用pptxgenjs和chartjs一键导出图表
- kubernetes - 将 Rancher 节点与公共 ID 连接的安全问题
- vb.net - 当我查看内容时,“字典中不存在密钥”,密钥在那里
- git - 通过 git 文件 (GIT) 公开完整的源代码
- javascript - 如何在 xamarin 形式的 android 中使用 wavesurfer.js?有人有想法吗?
- typescript - Mongoose 比较 BCRYPT 密码未填写
- syntax - 如何标记条件编译的使用语句?
- git - 在 git rebase 历史仍然不是线性之后
- smalltalk - 字符串或整个字符串的前 n 个字符;没有 SubscriptOutOfBounds
- javascript - e.preventDefault() 不适用于 MVC 中的提交按钮和锚标记