如果用户名和密码正确,我需要打开一个新表单,但我无法让此代码工作,如果我输入正确的用户名或密码,它什么也不做。
private void login_Click(object sender, EventArgs e)
{
try
{
string connection = @"Data Source=DX-PC;Initial Catalog=login;Integrated Security=True";
SqlConnection cn = new SqlConnection(connection);
cn.Open();
string userText = user.Text;
string passText = pass.Text;
SqlCommand cmd = new SqlCommand("SELECT ISNULL(Username, '') AS Username, ISNULL(Password,'') AS Password FROM log WHERE Username = @username and Password = @password", cn);
cmd.Parameters.Add(new SqlParameter("username", userText));
cmd.Parameters.Add(new SqlParameter("password", passText));
SqlDataReader dr = cmd.ExecuteReader();
try
{
dr.Read();
if (dr["Username"].ToString().Trim() == userText && dr["Password"].ToString().Trim() == passText)
{
MessageBox.Show("This message won't Display");
}
}
catch
{
MessageBox.Show("Invalid Username or Password");
}
dr.Close();
cn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}