unable to connect to any of the specified mysql hosts. c#

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

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

This is the exception text:

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 :)

回答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.

Working connection string:

"Persist Security Info=False;server=127.0.0.1;database=xx;uid=yy;password=zz" 


回答2:

The Problem with establishing connection to the remote site will be solved by providing public ip of that db instead of private one.

it will help u. Try it.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!