无法连接到任何指定的MySQL主机

时间:2012-05-16 12:39:19

标签: c# mysql

我正在尝试创建一个连接到MySQL数据库的登录表单。我安装了在表单中插入的sql连接器但是当我尝试连接时,我得到错误无法连接到任何指定的MySQL主机。这是我的代码:

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

        }




}
}

6 个答案:

答案 0 :(得分:3)

This site在连接字符串方面非常有用。您的连接字符串似乎无效。

另外:确保您的用户具有适当的访问权限。许多托管服务提供商仅允许从localhost访问。您可能需要请求它们使您的用户能够进行远程访问。

答案 1 :(得分:1)

我的MySQL连接字符串是:

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

您的连接字符串会引发什么异常?应该有一个错误号码。

答案 2 :(得分:0)

尝试样本中给定格式的连接字符串:

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

答案 3 :(得分:0)

我遇到了同样的问题,错误发生在连接字符串中!不知何故,我向两个不同的位置宣布它两次。一次到我的localhost和第二次到服务器机器数据库。当我在客户端计算机上发布项目时,无法连接到localhost(我的计算机是什么)。:-)我想知道如何得到这个:“无法连接到任何指定的mysql主机”

这就是人类可以让他的生活变得复杂的日子!: - )

当然,我能够连接到服务器机器上的db。

所以,例如,这是我的问题。并且,解决方案是向其声明一次和一个数据库!!!

而且,我想分享我的连接字符串,它工作正常:

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

答案 4 :(得分:0)

你的字符串连接是正确的没有错误,但请在本地服务器上检查它 这是例外只提高你的xampp没有运行,首先启动xampp 转到浏览器并键入localhost,如果它正常工作,您将看到xampp菜单 如果它打开localhost然后尝试连接你的服务器

string connection = "server=localhost;database=student_record_database;user=root;password=;";
            MySqlConnection con = new MySqlConnection(connection);

答案 5 :(得分: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;
    }
}