Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using MySql.Data.MySqlClient;
- namespace RegisterSystem
- {
- public partial class Form2 : Form
- {
- public const int WM_NCLBUTTONDOWN = 0xA1;
- public const int HT_CAPTION = 0x2;
- [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
- public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
- [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
- public static extern bool ReleaseCapture();
- private MySqlConnection conn;
- private string server;
- private string database;
- private string uid;
- private string password;
- public Form2()
- {
- server = "***********";
- database = "loginsystem";
- uid = "root";
- password = "***********";
- string connString;
- connString = $"SERVER=;{server}DATABASE=;{database}UID=;{uid}PASSWORD=;{password};";
- conn = new MySqlConnection(connString);
- InitializeComponent();
- }
- private void appClose_Click(object sender, EventArgs e)
- {
- Form1 loginForm = new Form1();
- loginForm.Close();
- this.Close();
- }
- private void Form2_MouseDown(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- ReleaseCapture();
- SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
- }
- }
- private void appMinimize_Click(object sender, EventArgs e)
- {
- this.WindowState = FormWindowState.Minimized;
- }
- public bool Register(string user, string pass, string email)
- {
- string query = $"INSERT INTO users (id, username, password, email) VALUES ('', '{user}', '{pass}', '{email}');";
- try
- {
- if (OpenConnection())
- {
- MySqlCommand cmd = new MySqlCommand(query, conn);
- try
- {
- cmd.ExecuteNonQuery();
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
- }
- else
- {
- conn.Close();
- return false;
- }
- }
- catch(Exception ex)
- {
- conn.Close();
- return false;
- }
- }
- private bool OpenConnection()
- {
- try
- {
- conn.Open();
- return true;
- }
- catch(MySqlException ex)
- {
- switch (ex.Number)
- {
- case 0:
- MessageBox.Show("Connection to the server failed!");
- break;
- case 1045:
- MessageBox.Show("Server username or password is incorrect!");
- break;
- }
- return false;
- }
- }
- private void regButton_Click(object sender, EventArgs e)
- {
- if(passBox.Text == passBox2.Text)
- {
- if (loginBox.TextLength <= 10)
- {
- if (emailBox.Text.Contains("@")){
- //
- string user = loginBox.Text;
- string pass = passBox.Text;
- string email = emailBox.Text;
- if(Register(user, pass, email)){
- MessageBox.Show($"User {user} has been created!");
- }
- else
- {
- MessageBox.Show($"User {user} has not been created!");
- }
- //
- }
- else
- {
- passBox2.Text = "";
- passBox.Text = "";
- MessageBox.Show("You don´t entered a right email!");
- }
- }
- else
- {
- passBox2.Text = "";
- loginBox.Text = "";
- passBox.Text = "";
- MessageBox.Show("Your username is more than 10 letters!");
- }
- }
- else
- {
- passBox2.Text = "";
- passBox.Text = "";
- MessageBox.Show("The passwords do not match");
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement