gpt4 book ai didi

c# - 如何制作登录系统C#

转载 作者:搜寻专家 更新时间:2023-10-30 22:01:12 24 4
gpt4 key购买 nike

关于登录系统,我的代码有问题。当我在数据库中添加 1 个用户名和密码时,它可以正常工作。但是当我在数据库中添加另外 1 个用户名和密码时,我的 else 语句将弹出两次。当 3 用户名和密码时,我的 else 语句将弹出 trice。依此类推......这是我的代码..如果你有比我更好的代码,请展示它。

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);
}
}
}
}

最佳答案

您的 while 循环会检查数据库中的每个用户,因此它一次命中正确的用户,两次命中错误的用户。要按照您现在编写的代码的方式执行此操作,您必须在 while 循环中使用一个标志来跟踪您是否找到了正确的用户/密码组合。像这样的东西:

        bool foundUser = false;

while (reader.Read())
{
if (tbUsername.Text == (reader["Username"].ToString()) && tbPassword.Text == (reader["Password"].ToString()))
{
foundUser = true;
break;
}
}


if (foundUser) {
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);
}

但这是一种非常低效的代码使用。相反,let SQL handle the filter for you .这就是它的设计目的。

        con.Open();

SqlCommand da = new SqlCommand("SELECT * FROM RegistrationTable WHERE Username=@Username AND Password=@Password", con);
da.Parameters.Add("@Username ", SqlDbType.Varchar);
da.Parameters["@Username "].Value = tbUsername.Text;
da.Parameters.Add("@Password", SqlDbType.Varchar);
da.Parameters["@Password"].Value = tbPassword.Text;

SqlDataReader reader = null;
reader = da.ExecuteReader();

if (da.HasRows)
{
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);
}
}

关于c# - 如何制作登录系统C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13106985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com