Advertisement
Guest User

Untitled

a guest
Oct 4th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.08 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 RegisterSystem
  13. {
  14.     public partial class Form2 : Form
  15.     {
  16.  
  17.         public const int WM_NCLBUTTONDOWN = 0xA1;
  18.         public const int HT_CAPTION = 0x2;
  19.  
  20.         [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
  21.         public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
  22.         [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
  23.         public static extern bool ReleaseCapture();
  24.  
  25.         private MySqlConnection conn;
  26.         private string server;
  27.         private string database;
  28.         private string uid;
  29.         private string password;
  30.  
  31.         public Form2()
  32.         {
  33.             server = "***********";
  34.             database = "loginsystem";
  35.             uid = "root";
  36.             password = "***********";
  37.  
  38.             string connString;
  39.             connString = $"SERVER=;{server}DATABASE=;{database}UID=;{uid}PASSWORD=;{password};";
  40.  
  41.             conn = new MySqlConnection(connString);
  42.  
  43.             InitializeComponent();
  44.         }
  45.  
  46.         private void appClose_Click(object sender, EventArgs e)
  47.         {
  48.             Form1 loginForm = new Form1();
  49.             loginForm.Close();
  50.             this.Close();
  51.         }
  52.  
  53.         private void Form2_MouseDown(object sender, MouseEventArgs e)
  54.         {
  55.             if (e.Button == MouseButtons.Left)
  56.             {
  57.                 ReleaseCapture();
  58.                 SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
  59.             }
  60.         }
  61.  
  62.         private void appMinimize_Click(object sender, EventArgs e)
  63.         {
  64.             this.WindowState = FormWindowState.Minimized;
  65.         }
  66.  
  67.         public bool Register(string user, string pass, string email)
  68.         {
  69.             string query = $"INSERT INTO users (id, username, password, email) VALUES ('', '{user}', '{pass}', '{email}');";
  70.  
  71.             try
  72.             {
  73.                 if (OpenConnection())
  74.                 {
  75.                     MySqlCommand cmd = new MySqlCommand(query, conn);
  76.  
  77.                     try
  78.                     {
  79.                         cmd.ExecuteNonQuery();
  80.                         return true;
  81.                     }
  82.                     catch (Exception ex)
  83.                     {
  84.                         return false;
  85.                     }
  86.                 }
  87.                 else
  88.                 {
  89.                     conn.Close();
  90.                     return false;
  91.                 }
  92.             }
  93.             catch(Exception ex)
  94.             {
  95.                 conn.Close();
  96.                 return false;
  97.             }
  98.         }
  99.  
  100.         private bool OpenConnection()
  101.         {
  102.             try
  103.             {
  104.                 conn.Open();
  105.                 return true;
  106.             }
  107.             catch(MySqlException ex)
  108.             {
  109.                 switch (ex.Number)
  110.                 {
  111.  
  112.                     case 0:
  113.                         MessageBox.Show("Connection to the server failed!");
  114.                         break;
  115.                     case 1045:
  116.                         MessageBox.Show("Server username or password is incorrect!");
  117.                         break;
  118.                         }
  119.                 return false;
  120.             }
  121.         }
  122.  
  123.         private void regButton_Click(object sender, EventArgs e)
  124.         {
  125.             if(passBox.Text == passBox2.Text)
  126.             {
  127.                 if (loginBox.TextLength <= 10)
  128.                 {
  129.                     if (emailBox.Text.Contains("@")){
  130.                         //
  131.  
  132.                         string user = loginBox.Text;
  133.                         string pass = passBox.Text;
  134.                         string email = emailBox.Text;
  135.  
  136.                         if(Register(user, pass, email)){
  137.                             MessageBox.Show($"User {user} has been created!");
  138.                         }
  139.                         else
  140.                         {
  141.                             MessageBox.Show($"User {user} has not been created!");
  142.                         }
  143.  
  144.                     //
  145.                     }
  146.                     else
  147.                     {
  148.                         passBox2.Text = "";
  149.                         passBox.Text = "";
  150.                         MessageBox.Show("You don´t entered a right email!");
  151.                     }
  152.                 }
  153.                 else
  154.                 {
  155.                     passBox2.Text = "";
  156.                     loginBox.Text = "";
  157.                     passBox.Text = "";
  158.                     MessageBox.Show("Your username is more than 10 letters!");
  159.                 }
  160.             }
  161.             else
  162.             {
  163.                 passBox2.Text = "";
  164.                 passBox.Text = "";
  165.                 MessageBox.Show("The passwords do not match");
  166.             }
  167.         }
  168.  
  169.         private void button1_Click(object sender, EventArgs e)
  170.         {
  171.  
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement