简体   繁体   中英

Cannot remotely connect to database

Im receiving a MySqlException was unhandled on a line: 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:

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.

According to their live support, they do not offer Remote Connection to MySQL on the free packages.

在此处输入图片说明

Shared web hosts almost never allow remote access to shared resources such as MySQL. What you will need to do is install a copy of MySQL in your local environment to do you development and testing. Then push your schema out through whatever tools they provide to you - these are usually web based. 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:

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

4) Do a try-catch statement to catch the errors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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