简体   繁体   English

无法远程连接到数据库

[英]Cannot remotely connect to database

Im receiving a MySqlException was unhandled on a line: conn.Open(); 我在一行上未处理收到MySqlException的异常:conn.Open();

        MySqlConnection conn = new MySqlConnection("host=fdb5.freehostingeu.com;user=1477630_one;password=******;database=1477630_one;");
        MySqlCommand cmd = new MySqlCommand("SELECT * FROM users WHERE name = '"+name+"'AND surname = '"+ surname +"';");
        cmd.Connection = conn;

        conn.Open();

I think the format of my connection string is wrong. 我认为我的连接字符串格式错误。 and I've tried altering the values to see whether it's the problem but I'm still unable to connect. 并且我尝试更改值以查看是否是问题所在,但仍然无法连接。

what am I doing wrong? 我究竟做错了什么?

ConnectionStrings.com shows this as the correct standard format: ConnectionStrings.com将此显示为正确的标准格式:

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

Adjusting yours accordingly: 相应地调整您的:

Server=fdb5.freehostingeu.com;Uid=1477630_one;Pwd=******;database=1477630_one;

See this other answer if you need to allow remote connections to MySQL. 如果您需要允许与MySQL的远程连接,请参见此其他答案

According to their live support, they do not offer Remote Connection to MySQL on the free packages. 根据他们的实时支持,他们没有在免费软件包中提供与MySQL的远程连接。

在此处输入图片说明

Shared web hosts almost never allow remote access to shared resources such as MySQL. 共享的Web主机几乎永远不允许远程访问共享资源,例如MySQL。 What you will need to do is install a copy of MySQL in your local environment to do you development and testing. 您需要做的是在本地环境中安装MySQL副本以进行开发和测试。 Then push your schema out through whatever tools they provide to you - these are usually web based. 然后通过提供给您的任何工具推出您的架构-这些通常是基于Web的。 Then when you push your site to the shared host you the connection string you are using should work fine. 然后,当您将站点推送到共享主机时,使用的连接字符串应该可以正常工作。

1) Be sure to add a reference to MySQL.Data 2) Include using MySql.Data.MySqlClient 3) Your connection string should be formatted as such: 1)确保添加对MySQL.Data的引用2) using MySql.Data.MySqlClient包含3)您的连接字符串应设置为以下格式:

connectionString = "SERVER=" + yourserver + ";" + "DATABASE=" + databasename + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

4) Do a try-catch statement to catch the errors 4)执行try-catch语句以捕获错误

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

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