0

I'm trying to create a login and register forum and store all of the usernames and passwords in a MySQL database. I found a free MySQL hosting site and instead of just having your databases listed I have the user name listed and then that users tables. I'm not really sure if doing it through a user is causing the problem or not. Here is the error I get.

Project Royal.vshost.exe Error: 0 : Unable to connect to any of the specified MySQL hosts. Exception thrown: 'MySql.Data.MySqlClient.MySqlException' in MySql.Data.dll

And also here is my code.

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 Project_Royal
{
    public partial class Login : Form
    {
        private MySqlConnection conn;
        private string server;
        private string database;
        private string uid;
        private string password;

        public Login()
        {
            server = "82.197.130.215";
            database = "2190559_users";
            uid = "2190559_users";
            password = "";

            string connString;
            connString = $"SERVER={server};DATABASE={database};UID={uid};PASSWORD={password};";

            conn = new MySqlConnection(connString);

            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string user = textBox1.Text;
            string pass = textBox2.Text;
            if (Register(user, pass))
            {
                MessageBox.Show($"User {user} has been created!");
            }
            else
            {
                MessageBox.Show($"User {user} has not been created!");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string user = textBox1.Text;
            string pass = textBox2.Text;
            if (IsLogin(user, pass))
            {
                MessageBox.Show($"Welcome {user}!");
            }
            else
            {
                MessageBox.Show($"{user} does not exist or password is incorrect!");
            }
        }

        public bool Register(string user, string pass)
        {
            string query = $"INSERT INTO Whitelisted (id, username, password) VALUE ('', '{user}', '{pass}');";

            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;
            }
        }

        public bool IsLogin(string user, string pass)
        {
            string query = $"SELECT * FROM Whitelisted WHERE username = '{user}' AND password = '{pass}');";

            try
            {
                if (OpenConnection())
                {
                    MySqlCommand cmd = new MySqlCommand(query, conn);
                    MySqlDataReader reader = cmd.ExecuteReader();

                    if (reader.Read())
                    {
                        reader.Close();
                        conn.Close();
                        return true;
                    }
                    else
                    {
                        reader.Close();
                        conn.Close();
                        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 server failed!");
                        break;
                    case 1045:
                        MessageBox.Show("Server username or password is incorrect!");
                        break;
                }
                return false;
            }
        }
    }
}
Panup Pong
  • 1,871
  • 2
  • 22
  • 44
Boot
  • 1
  • 2
  • Are you sure your connection string is OK? Check on https://www.connectionstrings.com/mysql/ for examples for library you're using. – mx0 Aug 26 '16 at 22:45
  • Possible duplicate of [Unable to connect to any of the specified mysql hosts. C# MySQL](http://stackoverflow.com/questions/17993657/unable-to-connect-to-any-of-the-specified-mysql-hosts-c-sharp-mysql) – Raktim Biswas Aug 26 '16 at 22:57
  • Don't use MySQL. It's garbage *(IMMCO)*!! – GreatAndPowerfulOz Aug 26 '16 at 23:40
  • @Great.And.Powerful.Oz What's a good free alternative then to MySQL? – Boot Aug 26 '16 at 23:44
  • Look [here](https://www.quora.com/What-are-the-best-MySQL-alternatives) or [here](http://stackoverflow.com/questions/326203/alternatives-to-mysql). There's also Microsoft's [SqlServer Express](https://www.microsoft.com/en-us/cloud-platform/sql-server-editions-express) – GreatAndPowerfulOz Aug 26 '16 at 23:50
  • There's also a free community edition of [MEMSql](http://memsql.com/) – GreatAndPowerfulOz Aug 26 '16 at 23:58
  • Please keep in mind that you showed the Internet your connection string. It may not be a root user, but please consider to delete that user. Also, for small Applications or databases, MySQL is more than Fitting and easy to host. – Cataklysim May 07 '18 at 09:28

0 Answers0