执行下面的代码时遇到问题。当我的用户名和密码为真时,我将收到此消息“无效的用户!使用有效的用户名和密码重试”。如果我输入错误的用户名 n 密码,则什么也不会发生(仍然无法进入下一个表单)。
private bool CompareStrings(string string1, string string2)
{
return String.Compare(string1, string2, true, System.Globalization.CultureInfo.InvariantCulture) == 0 ? true : false;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Server = blabla; Database = MoinMoun; User Id = Aema; password = 12345";
con.Open();
//SqlCommand cmd = new SqlCommand("SELECT username,password FROM Admin WHERE username='" + txtusername.Text + "' and password='" + txtpassword.Text + "'", con);SELECT ISNULL(username,'') AS username, ISNULL(password,'') AS password FROM Admin WHERE username = '" + txtusername.Text + "' and password='" + txtpassword.Text + "'", con
SqlCommand cmd = new SqlCommand("SELECT username,password FROM Admin WHERE username='" + txtusername.Text + "' and password='" + txtpassword.Text + "'", con);
SqlDataReader dr = cmd.ExecuteReader();
string usertext = txtusername.Text;
string passtext = txtpassword.Text;
while(dr.Read())
{
if(this.CompareStrings(dr["username"].ToString(), usertext) &&
this.CompareStrings(dr["password"].ToString(), passtext))
{
Form2 frm = new Form2();
frm.Show();
this.Hide();
}
else
{
MessageBox.Show("Invalid User! Try again with VALID username and password");
}
}
dr.Close();
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}