using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("SELECT realname, id, nivel FROM Users WHERE username = @username AND password = @password", connection);
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", password);
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
int id = reader.GetInt32(0);
string realname = reader.GetString(1);
string nivel = reader.GetString(2);
// create a new User object with the retrieved data
loggedInUser = new UserData(id, realname, nivel);
// open the main menu form
MainMenu mainMenu = new MainMenu(loggedInUser);
mainMenu.Show();
this.Hide();
}
else
{
// invalid credentials, display error message
MessageBox.Show("Credenciais inválidas!");
}
reader.Close();
}