无法连接到任何指定的MySQL主机 [英] unable to connect to any of the specified MySQL hosts

查看:1487
本文介绍了无法连接到任何指定的MySQL主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建连接到MySQL数据库的登录表单。我安装插入表格的SQL连接器,但是当我尝试连接,我得到的错误无法连接到任何指定的MySQL主机。这里是我的代码:

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;
使用MySql.Data.MySqlClient;

命名空间ECBSRecruitmentAgencySoftware
{
公共部分类登录:表
{
公共登录()
{
的InitializeComponent() ;
}

公共BOOL tryLogin(用户名字符串,字符串密码)
{
的MySqlConnection CON =新的MySqlConnection(主机= think-tek.net;用户= ctutorial ;密码= chang3d;数据库= ctutorial_logintest;);
的MySqlCommand CMD =新的MySqlCommand(SELECT * FROM登录其中用户名='+用户名+'AND user_pass ='+密码+';);
cmd.Connection = CON;
con.Open();
MySqlDataReader将读卡器= cmd.ExecuteReader();
如果(reader.Read()!= FALSE)
{
如果(reader.IsDBNull(0)== true)而
{
cmd.Connection.Close ();
reader.Dispose();
cmd.Dispose();
返回FALSE;
}
,否则
{
cmd.Connection.Close();
reader.Dispose();
cmd.Dispose();
返回真;
}
}
,否则
{
返回FALSE;
}
}
私人无效的button1_Click(对象发件人,EventArgs五)
{

如果(tryLogin(textBox1.Text,textBox2.Text)= = TRUE)
{
MainScreen F2 =新MainScreen();
F2.Show();
this.Hide();
}

,否则MessageBox.Show(错误的详细信息!);

}




}
}


解决方案

本网站是在连接字符串方面非常有帮助的。您的连接字符串似乎是无效的。



另外:请确保您的用户具有适当的访问权限。许多托管提供商只启用从本地主机访问。您可能不得不要求他们启用了远程接入用户。


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.

这篇关于无法连接到任何指定的MySQL主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆