无法连接到任何指定的MySQL主机。 C# [英] unable to connect to any of the specified mysql hosts. c#
问题描述
我发现很多建议来解决这个问题未处理的异常,我已经试过每一个我能找到的(和理解)。它一直小时,但仍然没有结果...我创建了一个Windows窗体应用程序要求一个客户的信息,我试图将这个信息到我曾在MySQL中创建一个数据库。这是一个似乎是工作的每个人,但代码我:
I've found many suggestions to fix this unprocessed exception, and I have tried every one I could find (and understand). It's been hours, but still no results... I've created a windows forms application that asks for a client's information and I'm trying to Insert this info into a DB I have created in MySQL. This is the code that seems to be working for everyone but me:
public partial class Form1 : Form
{
private MySqlConnection mConn;
public Form1()
{
InitializeComponent();
mConn = new MySqlConnection("Persist Security Info=False; server=localhost; database=xxxxxxx;uid=yyyyyyy;password=zzzzzzzz");
}
private void button1_Click(object sender, EventArgs e)
{
mConn.Open();
if (mConn.State == ConnectionState.Open)
{
MySqlCommand command = new MySqlCommand("INSERT INTO client VALUES (" + textBox1.Text + ",' " + textBox2.Text + "', '" + textBox3.Text + "'," + textBox4.Text + ",' " + textBox5.Text + "')", mConn);
command.ExecuteNonQuery();
mConn.Close();
}
else
MessageBox.Show("Connection Failed!");
}
}
这是异常文本:
MySql.Data.MySqlClient.MySqlException(0X80004005):无法连接到任何指定的MySQL主机。 ---> System.Net.Sockets.SocketException(0X80004005):请求ADRESS是在此上下文中有效[:: 1]:3306
在...
MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. ---> System.Net.Sockets.SocketException (0x80004005): The requested adress is not valid in this context [::1]:3306 in ...
如果你能帮助我,我会非常感激。这是我第一年的节目,至今它已经过去了相当不错,但现在我已经在我的课留下,因为这经常性的错误。预先感谢您:)
If you can help me I'll be very grateful. It's my first year programming and so far it has passed quite well, but now I have been "left behind" in my class because of this recurring error. Thank you in advance :)
推荐答案
我的教授是能够发现问题。出于某种原因,我的电脑无法识别术语localhost作为IP地址127.0.0.1,所以当我在写的服务器=本地主机在连接字符串中,发生了错误。当我试图写的服务器= 127.0.0.1 一切工作顺利。我希望这是有益
My Professor was able to find the problem. For some reason my computer can't recognize the term localhost as the IP address 127.0.0.1, so when I was writing server = localhost in the connection string, an error occurred. When I tried writing server = 127.0.0.1 everything worked smoothly. I hope this is helpful.
工作的连接字符串:
"Persist Security Info=False;server=127.0.0.1;database=xx;uid=yy;password=zz"
这篇关于无法连接到任何指定的MySQL主机。 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!