【发布时间】:2013-12-10 17:53:23
【问题描述】:
我无法将输入的用户名和密码与数据库中指定用户的用户名和密码进行比较。我相信所有代码都是正确的,但是当我单击登录按钮时,什么也没有发生。请帮助,如果您需要任何其他信息,请询问。
代码:
private void btnLogin_Click(object sender, EventArgs e)
{
int numerror = 0;
if (UsernameTextBox.Text == "")
{
numerror = numerror + 1;
}
if (PasswordTextBox.Text == "")
{
numerror = numerror + 1;
}
if (numerror == 1)
{
ErrorLabel.Text = "*1 required field is blank.";
}
else if (numerror == 2)
{
ErrorLabel.Text = "*2 required fields are blank";
}
else
{
string connectionString = "datasource=localhost;port=3306;username=*****;password=**********";
string select = "SELECT username, password FROM userinfo.users " +
"WHERE username = @username AND password = @password";
using (MySqlConnection Conn = new MySqlConnection(connectionString))
{
using (MySqlCommand cmd = new MySqlCommand(select, Conn))
{
cmd.Parameters.AddWithValue("@username", UsernameTextBox.Text);
cmd.Parameters.AddWithValue("@password", PasswordTextBox.Text);
Conn.Open();
using (MySqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
string username = reader.GetString(0);
string password = reader.GetString(1);
if (username == UsernameTextBox.Text)
{
string encodeduserinputpassword = EncodePassword(PasswordTextBox.Text);
if (password == encodeduserinputpassword)
{
AirSpace airspaceform = new AirSpace();
airspaceform.Show();
this.Hide();
}
else
{
CMessageBox("Login Error", "Incorrect username or password.");
}
}
else
{
CMessageBox("Login Error", "Incorrect username or password.");
}
}
}
Conn.Close();
}
}
}
}
【问题讨论】: