C# 无法连接到任何指定的MySQL主机
我正在尝试创建一个连接到MySQL数据库的登录表单。我安装了表单中插入的sql连接器,但当我尝试连接时,出现错误,无法连接到任何指定的MySQL主机。这是我的密码:C# 无法连接到任何指定的MySQL主机,c#,mysql,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 Syste
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!");
}
}
}
在示例中尝试使用给定格式的连接字符串:
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;
}
}
我的MySQL连接字符串是:
string mySqlConn = "server=localhost;user=username;database=databasename;port=3306;password=password;";
为您的连接字符串引发了什么异常?应该有一个错误号码 在连接字符串方面非常有用。您的连接字符串似乎无效
另外:确保您的用户具有适当的访问权限。许多托管提供程序只允许从本地主机进行访问。您可能需要请求它们允许您的用户进行远程访问。我也遇到了同样的问题,连接字符串中出现了错误!不知怎的,我向两个不同的地方申报了两次。一次到本地主机,第二次到服务器计算机数据库。当我在客户机上发布该项目时,无法连接到localhost(我的计算机是什么)。:-)我想知道如何得到这样的结果:“无法连接到任何指定的mysql主机” 这就是人类如何让自己的生活变得复杂了好几天:-) 当然,我能够连接到服务器机器上的db 举个例子,这就是我的问题。而且,解决方案是向它声明一次并向一个数据库声明 另外,我想与大家分享一下我的连接字符串,它工作得很好:
cs=@“服务器=xxx.xxx.xxx.xxx;uid=xxxx;密码=xxxxxxxxx;数据库=xxxxx;端口=3306”您的字符串连接正确无误,但首先在本地服务器上检查 这是一个例外,只有当xampp未运行时,才启动xampp 转到browser并键入localhost,如果它起作用,您将看到xampp菜单 如果它打开localhost,则尝试连接任何服务器
string connection = "server=localhost;database=student_record_database;user=root;password=;";
MySqlConnection con = new MySqlConnection(connection);
首先,注意不要在问题中发布敏感信息(主机/密码)。错误是什么?您会遇到什么异常?所有敏感信息都被替换为不真实的信息。@当我调试程序并尝试与用户登录时,aleroot在con.Open()上给了我错误,它说无法连接到任何指定的MySQL主机。像guy一样查看教程吗?连接和查询是相同的:)如果我使用此代码,它将如何检查用户或密码是否正确?我要求替换我在回答示例中给出的连接字符串。仍然得到相同的错误。因为我使用的是托管服务,我从那里获取数据库,你认为这会导致问题吗?“无法连接到任何指定的MySQL主机。”这是一个例外,它是一个通用的,没有数字。这是一个重复的[link]帖子,其中声明使用我和其他人发布的连接字符串。确保指定端口号。3306的默认端口,但如果您在MySQL安装中更改了它,它可能会有所不同。不确定为什么这个答案没有更多的+1…这就是为我解决这个问题的答案。
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;
}
}