kshadow22

Log Into Database From Application

Mar 11th, 2014
120
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.Windows.Forms;
  9. using MySql.Data.MySqlClient;
  10.  
  11. namespace FirstLogInData
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             try
  23. {
  24.  
  25. string myConnection = "datasource=localhost;port=3306;username=USERNAME;password=PASSWORD";
  26. MySqlConnection myConn = new MySqlConnection(myConnection);
  27.  
  28. MySqlCommand SelectCommand = new MySqlCommand("select * from DATABASE.TABLE where IDNAME='" + this.username_txt.Text + "' and Password='" + this.password_txt.Text + "' ;", myConn);
  29.  
  30. MySqlDataReader myReader;
  31. myConn.Open();
  32. myReader = SelectCommand.ExecuteReader();
  33. int count = 0;
  34. while (myReader.Read())
  35. {
  36. count = count + 1;
  37.  
  38. }
  39. if (count == 1)
  40.                 {
  41.  
  42. MessageBox.Show("Username and password is correct");
  43. }
  44. else if (count > 1)
  45. {
  46. MessageBox.Show("Duplication username and password ...Access Denied");
  47. }
  48. else
  49. MessageBox.Show("Username and password is Not correct ...Please try again");
  50. myConn.Close();
  51. }
  52. catch (Exception ex)
  53. {
  54.  
  55. MessageBox.Show(ex.Message);
  56. }
  57.         }
  58.     }
  59. }
RAW Paste Data