Advertisement
Guest User

Untitled

a guest
Jul 8th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.20 KB | None | 0 0
  1.         private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             //sql query
  4.             MySqlConnection conn;
  5.             MySqlDataAdapter da = new MySqlDataAdapter();
  6.             String sqlquery = "SELECT ID, username, password, banned FROM users Where username='" + textBox1.Text + "' and password='" + textBox2.Text + "'";
  7.             MySqlCommand myCommand = new MySqlCommand();
  8.             //connect to DB
  9.             conn = new MySqlConnection();
  10.             conn.ConnectionString = "server=127.0.0.1; uid=root; password=password; database=osssx;";
  11.             //see if connection failed.
  12.             try
  13.             {
  14.                 conn.Open();
  15.             }
  16.             catch (MySqlException myerror)
  17.             {
  18.                 MessageBox.Show("Error Connecting to Database: " + myerror.Message);
  19.                 this.Close();
  20.             }
  21.             myCommand.Connection = conn;
  22.             myCommand.CommandText = sqlquery;
  23.             //start query
  24.             da.SelectCommand = myCommand;
  25.             MySqlDataReader myData;
  26.             myData = myCommand.ExecuteReader();
  27.             //see if user exits.
  28.             {
  29.                 if (myData.HasRows)
  30.                 {
  31.                     MessageBox.Show("Valid Login Details", "Accepted", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  32.                     myData.Close();
  33.                     MySqlDataReader myReader;
  34.                     myReader = myCommand.ExecuteReader();
  35.                     while (myReader.Read())
  36.                     {
  37.                         ID = myReader.GetInt32(1);
  38.                         ID2 = myReader.GetString(1);
  39.                     }
  40.                     myReader.Close();
  41.                     Form2 main = new Form2();
  42.                     main.Show();
  43.                     this.Hide();
  44.  
  45.  
  46.                 }//if not close connection
  47.                 else
  48.                 {
  49.                     conn.Close();
  50.                     MessageBox.Show("Invalid Login Details, Retry?", "Login Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
  51.                     textBox1.Text = "";
  52.                     textBox2.Text = "";
  53.                 }
  54.  
  55.             }
  56.  
  57.  
  58.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement