【发布时间】:2018-10-08 02:09:17
【问题描述】:
我很难解决它并尝试了很多次,但仍然无法正常工作。这是场景,我有一个包含用户名和密码的登录表单。我有一个用于创建用户的数据库,用户类型为管理员和员工。我想要发生的是获取用户的用户名和用户类型并将其传递给另一种形式的标签。
这是我的代码
private static int count = 0;
private void btn_login_Click(object sender, EventArgs e)
{
using (var con = SQLConnection.GetConnection())
{
var selectCommand = new SqlCommand("Select * from Users_Profile where Username= @Username and Password= @Password", con);
selectCommand.Parameters.Add("@Username", SqlDbType.VarChar, 50).Value = txt_username.Text;
selectCommand.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = txt_password.Text;
SqlDataReader dataReader;
dataReader = selectCommand.ExecuteReader();
var loginSuccess = false;
while (dataReader.Read())
{
loginSuccess = true;
}
if (string.IsNullOrEmpty(txt_username.Text) || string.IsNullOrEmpty(txt_password.Text))
{
MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
if (loginSuccess)
{
count = 0;
MetroMessageBox.Show(this, "Login Successful", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Hide();
var obj = new MainForm(this);
obj.Closed += (s, args) => this.Close();
obj.Show();
}
else
{
count += 1;
if (count == 3)
{
MetroMessageBox.Show(this, "You have exceeded maximum login attempts, Please wait 10 seconds", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);
txt_username.Enabled = false;
txt_password.Enabled = false;
btn_login.Enabled = false;
LoginAttempstimeOut.Start();
}
else
{
MetroMessageBox.Show(this, "Invalid Username/Password", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
}
}
}
private void LoginAttempstimeOut_Tick(object sender, EventArgs e)
{
LoginAttempstimeOut.Stop();
txt_username.Enabled = true;
txt_password.Enabled = true;
btn_login.Enabled = true;
count = 0;
}
【问题讨论】:
-
@Z.R.T.很明显。这就是他在这里问的原因。如果你回答他的问题会更好:)
-
@Z.R.T.我知道我只是忘记了,我把它用在了我的其他代码中。