You can create a column in your database of type boolean
for when it is logged in.
Adding the table:
ALTER TABLE tabela_usuarios ADD 'logado' boolean;
To put Boolean as true
When you open the form, you can use the event Form_Load()
in this way:
private void Form1_Load(object sender, EventArgs e)
{
if (checklogado())
{
string comando = "UPDATE tabela_usuarios SET logado=1 WHERE usuario=@Usuario";
var cmd = new MySqlCommand(comando, connection); //connection é a string da conexão.
cmd.Parameters.AddWithValue("@Usuario", usuario); //usuario seria o nome de usuário da pessoa logada.
cmd.ExecuteScalar();
}
else
{
MessageBox.Show("Você já está logado em outra máquina!", "Erro");
Environment.Exit(0);
}
}
bool checklogado()
{
string comando = "SELECT logado FROM tabela_usuarios WHERE usuario=@Usuario";
var cmd = new MySqlCommand(comando, connection);
cmd.Parameters.AddWithValue("@Usuario", usuario);
bool returno = Convert.ToBoolean(cmd.ExecuteScalar());
return retorno;
}
To put Boolean as false
when closing the form, you can use the event Form_FormClosing()
in this way:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
string comando = "UPDATE tabela_usuarios SET logado=0 WHERE usuario=@Usuario";
var cmd = new MySqlCommand(comando, connection); //connection é a string da conexão.
cmd.Parameters.AddWithValue("@Usuario", usuario); //usuario seria o nome de usuário da pessoa logada.
cmd.ExecuteScalar();
}
Have you ever thought of controlling a table in the database?
– Rodolpho Sa
I thought this way: I would create a record when the user accessed it and delete it when closing the system. But if the system shuts down unexpectedly, the registry would be created and block the user from accessing again.
– Marcel Pereira de Souza