Advertisement
Guest User

Untitled

a guest
Jul 12th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.48 KB | None | 0 0
  1.      private void CheckLogin()
  2.         {
  3.             try
  4.             {
  5.                 //START OF SQL
  6.  
  7.                 conn = new MySqlConnection(cs); // conn will now be equaled to a MysqlConnection with the String CS which is declared up above.
  8.                 string query = "SELECT * FROM accounts WHERE Username=@username AND Password=@password"; // Our query we are executing
  9.                 conn.Open(); // Opening the Database Connection
  10.                 MySqlCommand cmd = new MySqlCommand(query, conn); // Defining the MySQL Command
  11.                 cmd.Parameters.AddWithValue("@username", txtUsername.Text); // These two Make sure that the text fields add up with our queries
  12.                 cmd.Parameters.AddWithValue("@password", txtPass.Text);
  13.                 MySqlDataReader dr = cmd.ExecuteReader(); // This executes a reader to check if it all adds up
  14.                 if (dr.HasRows)
  15.                 {
  16.                     NotifyBox.Text = "SUCCESS: Logging in."; // Prints out Success message if succeeded then opens a new form and closes connection
  17.                     conn.Close();
  18.                 }
  19.                 else
  20.                 {
  21.                     NotifyBox.Text = "ERROR: Login Failed."; // Login has failed error message
  22.                     conn.Close();
  23.                 }
  24.  
  25.             }
  26.             catch (Exception ex)
  27.             {
  28.                 MessageBox.Show("ERROR: " + ex.Message); // Catches the exception if there is one
  29.             }
  30.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement