Advertisement
Guest User

Untitled

a guest
Apr 1st, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Windows.Forms;
  4. using MetroFramework.Forms;
  5. using MetroFramework;
  6. using System.Data.SqlClient;
  7. using MySql.Data.MySqlClient;
  8.  
  9. namespace WRLoader
  10. {
  11. public partial class Form1 : MetroForm
  12. {
  13. private MySqlConnection conn;
  14. private string server;
  15. private string database;
  16. private string uid;
  17. private string password;
  18.  
  19. public Form1()
  20. {
  21. server = "localhost";
  22. database = "wr";
  23. uid = "root";
  24. password = "";
  25.  
  26. string connString;
  27. connString = $"SERVER={server};DATABASE={database};UID={uid};PASSWORD={password};";
  28. conn = new MySqlConnection(connString);
  29. InitializeComponent();
  30. }
  31.  
  32. private bool OpenConnection()
  33. {
  34. try
  35. {
  36. conn.Open();
  37. return true;
  38. }
  39. catch (MySqlException ex)
  40. {
  41. switch (ex.Number)
  42. {
  43. case 0:
  44. MessageBox.Show("No Connection");
  45. break;
  46. case 1045:
  47. MessageBox.Show("Server username or password is incorrect!");
  48. break;
  49. }
  50. return false;
  51. }
  52. }
  53.  
  54. public bool IsLogin(string user, string pass)
  55. {
  56. string query = $"SELECT * FROM login WHERE username='{user}' AND password='{pass}';";
  57. try
  58. {
  59. if (OpenConnection())
  60. {
  61. MySqlCommand cmd = new MySqlCommand(query, conn);
  62. MySqlDataReader reader = cmd.ExecuteReader();
  63.  
  64. if (reader.Read())
  65. {
  66. reader.Close();
  67. conn.Close();
  68. return true;
  69. }
  70. else
  71. {
  72. reader.Close();
  73. conn.Close();
  74. return false;
  75. }
  76. }
  77. else
  78. {
  79. conn.Close();
  80. return false;
  81. }
  82. }catch (Exception ex)
  83. {
  84. conn.Close();
  85. return false;
  86. }
  87. }
  88.  
  89. private void metroButton1_Click(object sender, EventArgs e)
  90. {
  91. string user = metroTextBox1.Text;
  92. string pass = MetroTextBox2.Text;
  93. if(IsLogin(user, pass))
  94. {
  95. this.Hide();
  96. Main mm = new Main();
  97. mm.Show();
  98. }
  99. else
  100. {
  101. MessageBox.Show("Please use a valid login", "alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
  102. }
  103. }
  104. private void MetroTextBox2_TextChanged(object sender, EventArgs e)
  105. {
  106. MetroTextBox2.UseSystemPasswordChar = true;
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement