0

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?

Prix
  • 19,417
  • 15
  • 73
  • 132
hello world
  • 306
  • 1
  • 6
  • 28

4 Answers4

2

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.

Community
  • 1
  • 1
Icarus
  • 63,293
  • 14
  • 100
  • 115
  • This is correct format keep in mind you might also have to grant access to remote machines in the database itself if you still continue with connection errors. See this link for examples, http://stackoverflow.com/questions/8453993/how-to-access-mysql-from-a-remote-computer-not-localhost – Bearcat9425 Aug 08 '13 at 13:36
2

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

enter image description here

Prix
  • 19,417
  • 15
  • 73
  • 132
  • is there a free alternative? – hello world Aug 08 '13 at 13:51
  • 1
    @helloworld yes you can install MySQL on your PC :) I doubt any free hosting would allow anyone to have remotely access to their MySQL server or you can pay for a non-free hosting which offers it. – Prix Aug 08 '13 at 13:53
  • @helloworld if u want to read data from your live database let's say users information and things like that, you could make an API or password protection area on your site using PHP or other language you prefer and use your C# to login to it and grab the data that is also an option. – Prix Aug 08 '13 at 13:57
2

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.

iamkrillin
  • 6,798
  • 1
  • 24
  • 51
0

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

Irsal
  • 156
  • 6