简体   繁体   English

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

[英]c# Unable to connect to any of the specified MySQL hosts

I am trying to connect MySQL database on remote host htttp://www.tasiheel.com . 我试图在远程主机htttp://www.tasiheel.com上连接MySQL数据库。

This is my code 这是我的代码

try
    {
     string addClientQuery = "insert into clients values(null,@name,@sender,@user,@pass,@date)";
     MySqlConnection conn = new MySqlConnection("Server=http://tasiheel.com;Database=xxxxxx;Uid=xxxx;Pwd=xxxxx;");

     MySqlCommand addClientCmd = new MySqlCommand(addClientQuery, conn);
     addClientCmd.Parameters.AddWithValue("name", clinetNameTXT.Text);
     addClientCmd.Parameters.AddWithValue("sender", senderNameTXT.Text);

     addClientCmd.Parameters.AddWithValue("user", userNameTXT.Text);
     addClientCmd.Parameters.AddWithValue("pass", userPassTXT.Text);
     addClientCmd.Parameters.AddWithValue("date", DateTime.Now);
     conn.Open();
     addClientCmd.ExecuteNonQuery();
     RadMessageBox.Show("تم اضافة البيانات بنجاح");
     }
 catch (MySqlException ex)
     {
     MessageBox.Show(ex.Message);
     }

but I got the following error when trying to open connection 但尝试打开连接时出现以下错误

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

I can access the databse form php page on the server, but I can't from c# winform 我可以访问服务器上的数据库表单php页面,但我不能从c#winform

I have enabled the remote database access by adding % to database privileges. 我通过向数据库权限添加%来启用远程数据库访问。

Any help for this? 对此有何帮助?

Thanks 谢谢

Try to change your connection string: 尝试更改连接字符串:

From: 从:

Server=http://tasiheel.com;Database=xxxxxx;Uid=xxxx;Pwd=xxxxx;

To

Server=http://tasiheel.com;Port=3306;Database=xxxxxx;User ID=xxxx;Password=xxxxx;

Try to enter the IP of the remote server in the connectionstring as described in another answer. 尝试在connectionstring中输入远程服务器的IP,如另一个答案中所述。

Make sure your MySQL (where it has installed) allow remote connection. 确保您的MySQL(已安装的地方)允许远程连接。 If database is on same server as code, you should better use localhost as Server (this will also save round trip to own server) 如果数据库与代码位于同一服务器上,则最好使用localhost作为Server(这样也可以节省往返服务器的往返)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM