简体   繁体   中英

unable to connect to any of the specified MySQL hosts

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!");

        }




}
}

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.

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM