Formulario Login

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

using System;

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

namespace Facturacion
{
public partial class frmlogin : Form
{
private string conn;
private MySqlConnection connect;
public frmlogin()
{
InitializeComponent();
}
private void db_connection()
{
try
{
conn = "Server=localhost; Database=facturacion; Uid=root; pwd=;";
connect = new MySqlConnection(conn);
connect.Open();
}
catch (MySqlException mysqlex)
{
MessageBox.Show(mysqlex.Number.ToString());
}
}

private bool validate_login(string user, string pass)


{

db_connection();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "Select * from usuario where user=@user and
pass=@pass";
cmd.Parameters.AddWithValue("@user", user);
cmd.Parameters.AddWithValue("@pass", pass);
cmd.Connection = connect;
MySqlDataReader ingreso = cmd.ExecuteReader();
if (ingreso.Read())
{
connect.Close();
return true;

}
else
{
connect.Close();
return false;
}
}

private void Form1_Load(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
string user = textBox1.Text;
string pass = textBox2.Text;
if (user == "" || pass == "")
{
MessageBox.Show("Falta algunos datos");
return;
}
bool r = validate_login(user, pass);
if (r)
{
connect.Close();
this.Hide();
frmInicio abrir = new frmInicio();
abrir.Show();
}
else
MessageBox.Show("Incorrecto");
}
}
}

You might also like