3

I am trying to create a login form connected to a MySQL database. I installed the sql connector inserted in the form but when I try to connect I get error unable to connect to any of the specified MySQL hosts. 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.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ECBSRecruitmentAgencySoftware
{
    public partial class LogIn : Form
    {
             public LogIn()
        {
            InitializeComponent();
        }

             public bool tryLogin(string username, string password)
             {
                 MySqlConnection con = new MySqlConnection("host=think-tek.net;user=ctutorial;password=chang3d;database=ctutorial_logintest;");
                 MySqlCommand cmd = new MySqlCommand("Select * FROM login WHERE user_name = `" + username + "` AND user_pass = `" + password + "`;");
                 cmd.Connection = con;
                 con.Open();
                 MySqlDataReader reader = cmd.ExecuteReader();
                 if (reader.Read() != false)
                 {
                     if (reader.IsDBNull(0) == true)
                     {
                         cmd.Connection.Close();
                         reader.Dispose();
                         cmd.Dispose();
                         return false;
                     }
                     else
                     {
                         cmd.Connection.Close();
                         reader.Dispose();
                         cmd.Dispose();
                         return true;
                     }
                 }
                 else 
                 {
                     return false;
                 }
             }
        private void button1_Click(object sender, EventArgs e)
        {

            if (tryLogin(textBox1.Text, textBox2.Text) == true)
            {
                MainScreen F2 = new MainScreen();
                F2.Show();
                this.Hide();
            }

             else MessageBox.Show("Wrong details!");

        }




}
}
aleroot
  • 71,077
  • 30
  • 176
  • 213
Nikolay Dyankov
  • 127
  • 1
  • 3
  • 11

6 Answers6

5

This site is pretty helpful in terms of connection strings. Your connection string seems to be invalid.

Also: Make sure your user has the proper access privileges. Many hosting providers only enable access from localhost. You may have to request that they enable your user for remote access.

Splatbang
  • 756
  • 1
  • 12
  • 29
1

My connection string for MySQL is:

string mySqlConn = "server=localhost;user=username;database=databasename;port=3306;password=password;";

What exception is thrown for your connection string? There should be a error number.

Robert H
  • 11,520
  • 18
  • 68
  • 110
  • "Unable to connect to any of the specified MySQL hosts." that is the exception it is a general one and there is no number – Nikolay Dyankov May 16 '12 at 12:54
  • This is a duplicate post of [link]http://stackoverflow.com/questions/4655669/unable-to-connect-to-any-of-the-specified-mysql-hosts[/link] where it states to use the connection string I and others have posted. Ensure you specify your port number. 3306 of the default port, but if you changed it on your MySQL install it may be different. – Robert H May 16 '12 at 12:57
0

Try the connection string in given format in the sample:

public bool tryLogin(string username, string password)
             {
                 MySqlConnection con = new MySqlConnection("SERVER=localhost;" +
                    "DATABASE=mydatabase;" +
                    "UID=testuser;" +
                    "PASSWORD=testpassword;");
                 MySqlCommand cmd = new MySqlCommand("Select * FROM login WHERE user_name = `" + username + "` AND user_pass = `" + password + "`;");
                 cmd.Connection = con;
                 con.Open();
                 MySqlDataReader reader = cmd.ExecuteReader();
                 if (reader.Read() != false)
                 {
                     if (reader.IsDBNull(0) == true)
                     {
                         cmd.Connection.Close();
                         reader.Dispose();
                         cmd.Dispose();
                         return false;
                     }
                     else
                     {
                         cmd.Connection.Close();
                         reader.Dispose();
                         cmd.Dispose();
                         return true;
                     }
                 }
                 else 
                 {
                     return false;
                 }
             }          
Romil Kumar Jain
  • 20,239
  • 9
  • 63
  • 92
0

I had the same problem and error was in connection string! Somehow, I declare it twice to two different locations. Once to my localhost and second time to server machine database. When I published the project on client machine it was impossible to connect to localhost(what was my computer).:-) and I was wondering how do I get this: "unable to connect to any of specified mysql hosts"

this is how man can make his life complicated for days!:-)

of course, i was able to connect to db on server machine.

So, just for example, this was my problem. And, solution was to declare to it once and to one database!!!

And also, I would like to share my connection string that worked just fine:

cs = @"server=xxx.xxx.xxx.xxx;uid=xxxx;password=xxxxxxxxx;database=xxxxx;port=3306";

ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
Sylca
  • 2,523
  • 4
  • 31
  • 51
0

your string connection is right there is no error,but fist check it on local server this is exception only raise your xampp is not running, first start xampp go to browser and type localhost,if its working you will see xampp menu if it open localhost then try to connect whatever your server is

string connection = "server=localhost;database=student_record_database;user=root;password=;";
            MySqlConnection con = new MySqlConnection(connection);
Adiii
  • 54,482
  • 7
  • 145
  • 148
0
public bool tryLogin(string username, string password)
{
    MySqlConnection con = new MySqlConnection("SERVER=xx.xx.xx.xx;DATABASE=dbname;UID=user;PASSWORD=password;CheckParameters=False;");
    MySqlCommand cmd = new MySqlCommand("Select * FROM login WHERE user_name = `" + username + "` AND user_pass = `" + password + "`;");
    cmd.Connection = con;
    con.Open();
    MySqlDataReader reader = cmd.ExecuteReader();
    if (reader.Read() != false)
    {
        if (reader.IsDBNull(0) == true)
        {
            cmd.Connection.Close();
            reader.Dispose();
            cmd.Dispose();
            return false;
        }
        else
        {
            cmd.Connection.Close();
            reader.Dispose();
            cmd.Dispose();
            return true;
        }
    }
    else 
    {
        return false;
    }
}