using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace agenda_de_Contatos
{
    public partial class frmLogin : Form
    {
        private MySqlConnection objCnx = new MySqlConnection();
        private MySqlCommand objCmd = new MySqlCommand();
        private MySqlDataReader objDados;

        public frmLogin()
        {
            InitializeComponent();
        }

        private void txtSair_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void txtEntrar_Click(object sender, EventArgs e)
        {

            try
            {
                string strSQL = "select * from tblusuario where loginUser = '" + txtUser.Text + "' and senhaUser = '" + txtSenha.Text + "'";
                objCmd.Connection = objCnx;
                objCmd.CommandText = strSQL;
                objDados = objCmd.ExecuteReader();

                if (objDados.HasRows)
                {
                    if (!objDados.IsClosed) { objDados.Close(); }
                    Form1 frmHome = new Form1();
                    frmHome.Show();
                    this.Close();
                    
                }
                else
                {
                    if (!objDados.IsClosed) { objDados.Close(); }
                    MessageBox.Show("Usuario ou senha invalidos!", "ADO.NET", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception Erro)
            {
                MessageBox.Show("Erro ao logar: " + Erro.Message);
            }
               
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            try
            {
                objCnx.ConnectionString = "Server=localhost;Database=bdcapacitacao;User=root;Pwd=neon321";
                objCnx.Open();
            }
            catch (Exception Erro)
            {
                MessageBox.Show(Erro.Message);

            }
        }
    }
}
