关于登录系统,我的代码有问题。当我在我的数据库中添加 1 个用户名和密码时,它可以正常工作。但是当我在数据库中添加另外 1 个用户名和密码时,我的 else 语句会弹出两次。当 3 个用户名和密码时,我的 else 语句将弹出三次。等等等等..这是我的代码..如果你有比我更好的代码,请出示。
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\JEDMARC\\Desktop\\VS v1.0.0\\Voting System v1.0.0\\Voting System v1.0.0.mdf;Integrated Security=True;User Instance=True");
SoundPlayer t = new SoundPlayer(@"C:\Users\JEDMARC\Documents\welcome.wav");
private void btnEnter_Click(object sender, EventArgs e)
{
if (cmbToE.Text == "HomeRoom Election" && comboBox1.Text == "English")
{
con.Open();
SqlCommand da = new SqlCommand("SELECT * FROM RegistrationTable", con);
SqlDataReader reader = null;
reader = da.ExecuteReader();
while (reader.Read())
{
if (tbUsername.Text == (reader["Username"].ToString()) && tbPassword.Text == (reader["Password"].ToString()))
{
MessageBox.Show("*Choose your best candidate. Use a Combobox.\n\n*After choosing, click Submit button to pass your vote!\n\n VOTE WISELY!", "How to vote?", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
UserHRForm x = new UserHRForm();
x.Show();
t.Play();
this.Close();
}
else
{
SystemSounds.Hand.Play();
MessageBox.Show("Access Denied! Account doesn't exist!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
}
}