View logged in user C #

1

I was able to get the user logged in using typed text in TextBox . But I do not want the user, I want the username.
Database example:

id = 1
nome_usuario = Administrador
usuario = admin
senha = 1234
nivel_acesso = 1

I want to get the data nome_usuario and display it in a Label on the main form of my application.

This is my form code frmLogin

public partial class frmLogin : Form
{

    public bool logado = false;
    public static string usuarioConectado;

    public frmLogin()
    {
        InitializeComponent();
    }

    private void btnEntrar_Click(object sender, EventArgs e)
    {
        string conexao = "Data Source=DESKTOP-AJLR3DB\SQLEXPRESS;Initial Catalog=DBGestor;Integrated Security=True";
        SqlConnection conn = new SqlConnection(conexao);
        SqlCommand comando = new SqlCommand("SELECT * FROM funcionarios WHERE usuario = @usuario and senha = @senha", conn);

        comando.Parameters.Add("@usuario", SqlDbType.NVarChar).Value = txtUsuario.Text;
        comando.Parameters.Add("@senha", SqlDbType.Int).Value = txtSenha.Text;

        conn.Open();

        int i = (int)comando.ExecuteScalar();

        if (i > 0)
        {
            logado = true;
            usuarioConectado = reader["nome_funcionario"].ToString();
            this.Dispose();
        }
        else
        {
            logado = false;
            MessageBox.Show("Usuário e/ou Senha inválido.");
        }
        conn.Close();
    }

    private void txtSenha_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!Char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
        {
            e.Handled = true;
            MessageBox.Show("Este campo aceita apenas números.");
        }
    }

    private void btnCancelar_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

I need to know how to assign the value nome_usuario to the variable usuarioConectado
This is my form code frmPricipal

private void frmPrincipal_Load(object sender, EventArgs e)
{
    lblUsuario.Text = "Olá, " + frmLogin.usuarioConectado;
}

I was able to resolve the problem, updated code.

    
asked by anonymous 15.04.2018 / 22:16

1 answer

2

To read values, you can use ExecuteReader , see how it would look (follow the comments):

var conn = new SqlConnection(conexao);
var comando = new SqlCommand("SELECT * FROM funcionarios WHERE usuario = @usuario and senha = @senha", conn);

comando.Parameters.Add("@usuario", SqlDbType.NVarChar).Value = txtUsuario.Text;
comando.Parameters.Add("@senha", SqlDbType.Int).Value = txtSenha.Text;

conn.Open();

var reader = comando.ExecuteReader(); //Executa o comando

if (reader.Read()) //Lê uma linha (false se não tiver linhas para ler)
{
    logado = true;
    usuarioConectado = reader["nome_usuario"].ToString(); //Pega o valor de "nome_usuario" da linha que foi lida
    this.Dispose();
}
else
{
    logado = false;
    MessageBox.Show("Usuário e/ou Senha inválido.");
}
conn.Close();

To tinker with database, I always prefer to use using .

    
16.04.2018 / 01:57