Advertisement
hiroyukims

C# .NET (WINDOWS FORM) Login System.

Dec 22nd, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using MySql.Data.MySqlClient;
  11.  
  12. namespace Grou
  13. {
  14.     public partial class Frm_Login : Form
  15.     {
  16.  
  17.         MySqlConnection conn = new MySqlConnection(Properties.Settings.Default.conexao);
  18.         private Boolean usertype;
  19.         public Frm_Login()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         private void Login()
  25.         {
  26.             if (!String.IsNullOrEmpty(TxtLogin.Text) || !String.IsNullOrEmpty(TxtSenha.Text))
  27.             {
  28.                 conn.Open();
  29.                 try
  30.                 {
  31.                     MySqlCommand cmd = new MySqlCommand("SELECT username,password,nivel FROM grou_login WHERE username = @user AND password = @pass", conn);
  32.                     cmd.Parameters.AddWithValue("@user", TxtLogin.Text); //parametros para usuario;
  33.                     cmd.Parameters.AddWithValue("@pass", TxtSenha.Text); //parametros para senha
  34.                     MySqlDataReader mdr = cmd.ExecuteReader();  //Data Reader (Adaptador para obj cmd)
  35.                     if (mdr.Read() == true)
  36.                     {
  37.                         usertype = Convert.ToBoolean(mdr["nivel"]);
  38.                         if (usertype == true)
  39.                         {
  40.                             MessageBox.Show("ADMIN LOGADO!");
  41.                         }
  42.                         else
  43.                         {
  44.                             MessageBox.Show("NORMAL USER LOGADO");
  45.                         }
  46.                        
  47.                     }
  48.                     else
  49.                     {
  50.                         MessageBox.Show("Nome de usuário ou senha inválidos, Por favor tente mais tarde.");
  51.                     }
  52.                 }
  53.                 catch (Exception ex)
  54.                 {
  55.                     MessageBox.Show("Ops! houve um erro desconhecido~~:", Convert.ToString(ex));
  56.                 }
  57.                 finally
  58.                 {
  59.                     conn.Close();
  60.                 }
  61.             }
  62.             else
  63.             {
  64.                 MessageBox.Show("Ops! Preencha os campos corretamente!"); //se campo for vazio
  65.             }
  66.            
  67.         }
  68.  
  69.         private void BtnLogin_Click(object sender, EventArgs e) //Btn_LOGIN
  70.         {
  71.             Login();
  72.         }
  73.  
  74.         private void BtnCancel_Click(object sender, EventArgs e) //Btn_CANCEL
  75.         {
  76.             Application.ExitThread();
  77.         }
  78.  
  79.         private void TxtLogin_KeyPress(object sender, KeyPressEventArgs e)
  80.         {
  81.             if (e.KeyChar == 13)
  82.             {
  83.                 Login();
  84.             }
  85.         }
  86.  
  87.         private void TxtSenha_KeyPress(object sender, KeyPressEventArgs e)
  88.         {
  89.             if (e.KeyChar == 13)
  90.             {
  91.                 Login();
  92.             }
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement