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;
using MySql.Data;

namespace EjemploMysql
{
    public partial class Login : Form
    {
        MySqlConnection conector;
        public Login()
        {
            conexionBD();
            InitializeComponent();
        }
        public void conexionBD()
        {
            conector=new MySqlConnection();
            conector.ConnectionString = "Server=localhost;Port=3306;DataBase=ejemploMysql;Uid=root;Pwd=123";
            try
            {
                conector.Open();
            }catch(MySqlException e){
                try
                { 
               MySqlConnection conector2 = new MySqlConnection();
                conector2.ConnectionString = "Server=localhost;Port=3306;DataBase=test;Uid=root;Pwd=123";
                conector2.Open();
                MySqlCommand comando = conector2.CreateCommand();
               

                    comando.CommandText = "create database ejemploMysql;";
                    comando.ExecuteNonQuery();
                    comando.CommandText = "use ejemplomysql;";
                    comando.ExecuteNonQuery();
                    comando.CommandText = "create table usuarios(usuario varchar(20) primary key, password varchar(20), tipo varchar(20));";
                    comando.ExecuteNonQuery();
                    comando.CommandText = "create table datos(idDatos int primary key auto_increment not null, nombre varchar(45), apellido varchar(45), telefono varchar(45));";
                    comando.ExecuteNonQuery(); 
                    comando.CommandText = "insert into usuarios values('admin','123','admin');";
                    comando.ExecuteNonQuery();
                    conector2.Close();
                    conexionBD();
                }catch(MySqlException e1){
                    MessageBox.Show(e1.Message.ToString());
                }
                e.ErrorCode.ToString();
            }
        }
        private void btnAcceso_Click(object sender, EventArgs e)
        {
            if (conector.State != ConnectionState.Open)
            {
                conector.Open();
            }
            MySqlCommand ejecutador=conector.CreateCommand();
            ejecutador.CommandText="select *from usuarios where usuario='"+txtUsuario.Text+"' and password='"+txtPassword.Text+"'";
            MySqlDataReader captura=ejecutador.ExecuteReader();
            if(captura.Read())
            {
                Parametros.usuario = captura.GetString(0);
                Parametros.tipo = captura.GetString(2);
                this.Hide();
                new principal().Show();
                
            }else{
                MessageBox.Show("Usuario y/o Contraseña Incorrectos");
            }
            conector.Close();
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            
        }
    }
}
