我正在使用 VS2010 构建一个 Windows 窗体应用程序。我想读取用户 ID 和密码并使用数据库检查它们的有效性。如果找到合适的匹配项,我想将用户引导至主表单。当我输入正确的凭据时,我的代码当前没有指示。可能是什么原因?
string connectionstring = "Data Source=localhost;Initial Catalog=HMS;Persist Security Info=True;User ID=Developer;Password=abc@123";
SqlConnection connection = new SqlConnection(connectionstring);
string SelectStatement = "SELECT * FROM Users where UserID = '@UserID' and Password = '@password'";
SqlCommand insertcommand = new SqlCommand(SelectStatement, connection);
insertcommand.Parameters.AddWithValue("@UserID", textBox1.Text);
insertcommand.Parameters.AddWithValue("@password", textBox2.Text);
SqlDataReader reader;
try
{
connection.Open();
reader = insertcommand.ExecuteReader();
while (reader.Read())
{
string userid = reader["UserID"].ToString();
string pass = reader["Password"].ToString();
if (userid == textBox1.Text.ToString() && pass == textBox2.Text.ToString()) // login successful
{
Mainform main = new Mainform();
this.Hide();
main.Show();
}
else
{
MessageBox.Show("Invalid Username or Password!");
}
}//end while
reader.Close();
}
catch (Exception ex)
{
throw ex;
}//end catch
finally
{
connection.Close();
}